似乎我在尝试在我的 DataGrid 上使用 DataTemplates 时碰壁了。我要做的是使用一个模板为每个单元格显示两行文本。但似乎不可能以任何方式绑定列。
以下代码有望显示我想要做的事情。注意每列的绑定:模板列没有这样的东西,因此,这个 xaml 不可能工作。
<Window.Resources>
<DataTemplate x:Key="DoubleField">
<StackPanel>
<TextBlock Text="{Binding Value1}"/>
<TextBlock Text="{Binding Value2}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<DataGrid>
<DataGrid.Columns>
<DataGridTemplateColumn CellTemplate="{StaticResource DoubleField}" Binding="{Binding Title}"/> // <- Binding does not exist for templatecolumn, I only wish it did
<DataGridTemplateColumn CellTemplate="{StaticResource DoubleField}" Binding="{Binding Price}"/> // <- Binding does not exist for templatecolumn, I only wish it did
<DataGridTemplateColumn CellTemplate="{StaticResource DoubleField}" Binding="{Binding Stuff}"/> // <- Binding does not exist for templatecolumn, I only wish it did
</DataGrid.Columns>
</DataGrid>
class MyListItem {
class DoubleItem {
string Value1 { get; set; }
string Value2 { get; set; }
}
DoubleItem Title { get; set; }
DoubleItem Price { get; set; }
DoubleItem Stuff { get; set; }
}
我是否注定要将整个 DataTemplate 复制到每一列,只是为了在每个副本上都有不同的绑定?当然有一个很好的方法来解决这个问题?还是我只是再次错过了一些明显的东西?