【问题标题】:Binding Textblock text inside a combobox grid cell在组合框网格单元格内绑定文本块文本
【发布时间】:2020-01-08 06:51:42
【问题描述】:

我通过数据模板在数据网格视图中使用组合框单元格。

我绑定了一个项目源并在里面的组合框上设置了 DisplayMemberPath 、 SelectedValuePath 和 SelectedValue 属性

一旦选择了组合框中的一个项目,我想在 textblock 元素上显示 DisplayMemberPath 文本,我只是不知道如何绑定它。

<DataGridTemplateColumn>

    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <ComboBox   
ItemsSource="{Binding Path=DataContext.PartNumbers, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                        DisplayMemberPath="PartNumberDescription"        
                        SelectedValuePath="PartNumberCodeCode"
                        SelectedValue="{Binding Code}"/>
          </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>

    <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                    <TextBlock Text="{How can I bind DisplayMemberPath here?}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>

如果我使用相同的 SelectedValue 绑定,它可以工作并显示值,但我想显示描述。

<TextBlock Text="{Binding Code}"/>
<!-- It works, but I would like to show the text of the combobox, not the value -->

【问题讨论】:

    标签: wpf binding datagridviewcomboboxcell


    【解决方案1】:

    XAML

    <TextBlock Text="{Binding Description}"/> 
    

    视图模型

    public string Description {
        get {
            return PartNumbers.SingleOrDefault(x => x.PartNumberCodeCode == Code)?.PartNumberDescription;
        }
    }
    

    Code的属性发生变化,通知Description的属性变化

    【讨论】:

      猜你喜欢
      • 2012-01-05
      • 1970-01-01
      • 2020-05-28
      • 2019-11-10
      • 1970-01-01
      • 2016-07-19
      • 2013-12-31
      • 2018-09-21
      • 1970-01-01
      相关资源
      最近更新 更多