2009-10-29 108 views
0

假设你有一个列表,看起来像下面数据绑定另一个数据绑定控件中 - 根据当前项目

public List<Person> Persons { get; set; } 

您在ListBox设置ItemSource摆脱人及数据该列表中的每个人都希望选择其他项目,具体取决于社会安全号码。

这不是将数据存储在Person中的选项,因此一旦在列表中处理Person,就必须提取数据。

这将最终看起来有点像这样OM XAML

<ListBox Name="myListBox"> 
    <ListBox.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Horizontal" /> 
     </ItemsPanelTemplate> 
    </ListBox.ItemsPanel> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <Label Content="{Binding Name}"/> 
       <ComboBox ItemsSource="{Binding MyOtherData}" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

现在,我想MyOtherData是一种方法,其将返回一组数据,根据当前的人,所以我只是想有一个方法,羚牛像社会安全号码的论点。

这看起来怎么样?

我对WPF-XAML的东西很陌生,如果这是一个设计缺陷,请提供其他解决方案。

回答

1

WPF的方式是使用转换器。做一个实现了IValueConverter的类,然后添加属性ValueConversion。在属性你从你转换哪种类型的陈述到,INT(安全号码是不大概一个int,但...),以大概某种名单的说,一个String数组:

[ValueConversion(typeof(int), typeof(string[]))] 
public class GetThatData : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     return new string[]{"just","for","test"}; 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

第二你需要导入命名空间(如果你还没有已经这样做了):

xmlns:local="clr-namespace:NamespaceWhereTheClassIs" 

三,创建一个类的对象:

<Window.Resources> 
    <local:GetThatData x:Key="otherData" /> 
</Window.Resources> 

而在去年申请转换白衣他值:

<ComboBox ItemsSource="{Binding Path=SSN, Converter={StaticResource otherData}}" /> 

只是纯粹的WPF魔法,希望你能使它工作