DataGrid で DataTemplates を使用しようとして、壁にぶつかったようです。私がやろうとしているのは、1 つのテンプレートを使用して、各セルに 2 行のテキストを表示することです。しかし、どのような方法でも列をバインドすることはできないようです。
次のコードは、私がやりたいことを示していることを願っています。各列のバインドに注意してください。テンプレート列にはそのようなものはなく、この 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 全体をすべての列にコピーする運命にありますか? 確かにこれを回避する良い方法はありますか?それとも、また盲目的に明白な何かを見逃しているのでしょうか?