2010-09-23 125 views
0

我是DataTemplating的列表框的ItemSource以显示一系列组合框。我想将组合的DisplayMemberPath赋予一个属性,该属性与它自己的ItemsSource不同。 (假设DisplayMemberPath只是一个表示属性名称的字符串,我从用户那里得到这个)。我用CollectionViewSource实现了这一点,但所有组合框都显示相同的列表。WPF DataTemplate和绑定 - 这可能在XAML?

什么我期待着有后的数据模板是有组合框显示,

ComboboxInstance1.DisplayMemberPath = PropertyMapOfEmployee in FilterControls[0] 
ComboboxInstance2.DisplayMemberPath = PropertyMapOfEmployee in FilterControls[1] 

这是可能在XAML实现?

谢谢。玛尼

用户控件:

<Resources> 
    <CollectionViewSource x:Key="bindingSource" Source="{Binding BindingItems}"/> 
    <CollectionViewSource x:Key="FilterSource" Source="{Binding FilterControls}"/> 

    <DataTemplate DataType="{x:Type CustomTypes:FilterElement}"> 
     <ComboBox ItemsSource="{Binding Source={StaticResource bindingEmp}" 
       DisplayMemberPath="{Binding Source={StaticResource FilterSource}, 
              Path=PropertyMapofEmployee}" /> 
    </DataTemplate> 

<Resources> 

--- 

<DockPanel> 
    <ListBox x:Name="lstBox" ItemsSource="{Binding FilterControls}" /> 
</DockPanel> 

视图模型:

List<FilterElement> FilterControls; 
List<Employee> Employees 

class FilterElement 
{ 
    string Caption; 
    String PropertyMapofEmployee 
} 

回答

3
<ComboBox ItemsSource="{Binding Source={StaticResource bindingEmp}" 
      DisplayMemberPath="{Binding PropertyMapofEmployee}" /> 
+0

我不认为PropertyMapOfEmployee是Employee类的一个属性,所以我不认为这会起作用。 – 2010-09-23 18:49:47

+0

谢谢..这实际上修复了原来的和当前的问题。 – 2010-09-23 18:53:52

+0

它工作原理是因为PropertyMapOfEmployee是FilterControls的一个特性,它绑定到ListBox(数据模型)。​​因此,组合绑定总是指父级,除非在绑定中使用'Source ='专门设置。因此它起作用。 – 2010-09-23 18:56:04

0

我不知道你能做到这一点的XAML。 (将DisplayMemberPath指向指向DataContext以外的对象的属性)。你可能想看看RelativeSource Class看看是否能满足你的需求。

您是否想过在您的Employee对象中向FilterElement提供一个引用,然后连接到您创建的Employee.PropertyMapOfEmployee属性的绑定?

+0

这是可能的。见下文。 – 2010-09-23 18:54:08