设置DataTemplate的DisplayMemberPath [英] Setting DisplayMemberPath of DataTemplate

查看:38
本文介绍了设置DataTemplate的DisplayMemberPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据模板:

<Window.Resources>
    <DataTemplate x:Key="MyDataGridCell_TextBox">
        <TextBlock Text="{Binding}" />
    </DataTemplate>
</Window.Resources>

以及我的DataGrid中的以下DataGridColumn:

And the following DataGridColumn in my DataGrid:

<DataGrid ItemsSource="{Binding Logs}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="A" CellTemplate="{StaticResource MyDataGridCell_TextBox} 
HOW_DO_I_SET_HERE_DisplayMember_To_PropA???"/> 
        <DataGridTemplateColumn Header="B" CellTemplate="{StaticResource MyDataGridCell_TextBox} 
HOW_DO_I_SET_HERE_DisplayMember_To_PropB???"/> 
    </DataGrid.Columns>
</DataGrid>

如何从DataGridTemplateColumn设置DataTemplate的DisplayMemberPath?

How can I set the DisplayMemberPath of the DataTemplate from DataGridTemplateColumn?

public class Log
{
    public string PropA {get;set;}
    public string PropB {get;set;}
}

Logs <=> ObservableCollection<Log>

PS:我删除了不相关的代码部分,例如样式,一些道具等,并尽我所能简化了代码.

PS: I removed the irrelevant code parts like styles, some props etc. and tried to simplify the code as much as I could.

推荐答案

您无法在XAML中执行此操作.您需要为每列定义一个 DataTemplate .

You can't do this in XAML. You need to define a DataTemplate per column.

无法仅更改模板中绑定的路径并保留其余内容.模板必须定义为一个整体.

There is no way to just change the path of a binding in a template and keep the rest. A template must be defined as a whole.

这篇关于设置DataTemplate的DisplayMemberPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆