我想创建一个数据网格单元模板,以在项目中的不同数据网格中使用它。它应该是单行,并且必须能够将视图模型中的值绑定到它。我已经对用户控件进行了类似的操作,但无法找到这种情况的解决方案...
我已经在这里尝试过文档解决方案:https://docs.microsoft.com/en-us/dotnet/api/system.windows.controls.datagridtemplatecolumn?view=netframework-4.8
我可以直接在这里绑定我的财产,只需替换???即可。与“ MyProperty”一起使用,但随后将无法用于其他属性:
<Page.Resources>
<DataTemplate x:Key="MyTextViewTemplate">
<TextBlock Text="{Binding ???}"/>
</DataTemplate>
<DataTemplate x:Key="MyTextEditTemplate">
<TextBox Text="{Binding ???}"/>
</DataTemplate>
</Page.Resources>
我想在下面的DataGridTemplateColumn中绑定此值:
<DataGrid Name="Test" ItemsSource="{Binding Path=MyItems}"...>
<DataGrid.Columns>
<DataGridTemplateColumn IsReadOnly="False" Header="My Header" CellTemplate="{StaticResource MyTextViewTemplate}" CellEditingTemplate="{StaticResource MyTextEditTemplate}" Binding=???/>
</DataGrid.Columns>
基本上我不明白,我该如何传递值(我可以在哪里绑定到DataGrid模板列中),以便它落入我的一个数据模板中... 感谢您的帮助!
编辑
有一种更好的常规方法,它非常复杂。完整的答案描述如下:WPF DataGrid - Creating a new custom Column