【问题标题】:WPF DataTemplate and Binding - Is this possible in xaml?WPF DataTemplate 和绑定 - 这在 xaml 中可行吗?
【发布时间】:2011-04-16 10:26:08
【问题描述】:

我正在对列表框的ItemSource 进行数据模板化以显示一系列组合框。我想将组合的DisplayMemberPath 提供给一个属性,该属性与它自己的ItemsSource 的来源不同。 (假设DisplayMemberPath 只是一个代表属性名称的字符串,我从用户那里得到这个)。我通过CollectionViewSource 实现了这一点,但所有组合框都显示相同的列表。

我期望在数据模板化之后显示组合框,

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
}

【问题讨论】:

    标签: wpf xaml wpf-controls binding


    【解决方案1】:

    我不确定您是否可以在 XAML 中做到这一点。 (让 DisplayMemberPath 指向 DataContext 以外的对象上的属性)。您可能想查看RelativeSource Class,看看这是否能满足您的需求。

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

    【讨论】:

      【解决方案2】:
      <ComboBox ItemsSource="{Binding Source={StaticResource bindingEmp}"
                DisplayMemberPath="{Binding PropertyMapofEmployee}" />
      

      【讨论】:

      • 我不认为 PropertyMapOfEmployee 是 Employee 类的属性,所以我认为这不会起作用。
      • 谢谢 .. 这实际上解决了原始问题和当前问题。
      • 之所以有效,是因为 PropertyMapOfEmployee 是 FilterControls 的一个属性,它绑定到列表框(它是数据模板)。因此,组合绑定始终引用父级,除非在绑定中使用 'Source=' 专门设置.因此它起作用了。
      • @Dave,Employee对象不是ComboBox的DataContext,而是ComboBoxItem的DataContext...
      猜你喜欢
      • 2013-03-01
      • 2011-02-15
      • 1970-01-01
      • 2012-11-14
      • 2014-06-06
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 2011-11-29
      相关资源
      最近更新 更多