データグリッド列で cell/celledit テンプレートを共有したい。
私はそれを行うソリューションを持っています ( WPF DataGridTemplateColumn 共有テンプレートのおかげで? )。今私が望んでいるのは、すべてのノードのネストを回避することで読みやすさを改善することです。
私の現在のビューは次のようになります。
<wpftk:DataGrid ItemsSource="{Binding Tests}" AutoGenerateColumns="False">
<wpftk:DataGrid.Resources>
<DataTemplate x:Key="CustomCellTemplate">
<TextBlock Text="{TemplateBinding Content}"/>
</DataTemplate>
<DataTemplate x:Key="CustomCellEditingTemplate">
<TextBox Text="{TemplateBinding Content}"></TextBox>
</DataTemplate>
</wpftk:DataGrid.Resources>
<wpftk:DataGrid.Columns>
<wpftk:DataGridTemplateColumn Header="Start Date">
<wpftk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ContentPresenter ContentTemplate="{StaticResource CustomCellTemplate}" Content="{Binding StartDate}"/>
</DataTemplate>
</wpftk:DataGridTemplateColumn.CellTemplate>
<wpftk:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ContentPresenter ContentTemplate="{StaticResource CustomCellEditingTemplate}" Content="{Binding StartDate}"/>
</DataTemplate>
</wpftk:DataGridTemplateColumn.CellEditingTemplate>
</wpftk:DataGridTemplateColumn>
<!--and again the whole block above for each columns...-->
</wpftk:DataGrid.Columns>
</wpftk:DataGrid>
私が達成したいのは、レベルで値をバインドし、DataGridTemplateColumn
それをテンプレート レベルに伝播することです。誰もそれを行う方法を知っていますか?
私がやろうとしたことは次のようなものです:
<wpftk:DataGrid ItemsSource="{Binding Tests}" AutoGenerateColumns="False">
<wpftk:DataGrid.Resources>
<DataTemplate x:Key="CustomCellTemplate">
<TextBlock Text="{Binding}"/>
</DataTemplate>
<DataTemplate x:Key="CustomCellEditingTemplate">
<TextBox Text="{Binding}"></TextBox>
</DataTemplate>
</wpftk:DataGrid.Resources>
<wpftk:DataGrid.Columns>
<wpftk:DataGridTemplateColumn Header="Start Date" Binding="{Binding StartDate}" CellTemplate="{StaticResource CustomCellTemplate}" CellEditingTemplate="{StaticResource CustomCellEditingTemplate}"/>
<wpftk:DataGridTemplateColumn Header="End Date" Binding="{Binding EndDate}" CellTemplate="{StaticResource CustomCellTemplate}" CellEditingTemplate="{StaticResource CustomCellEditingTemplate}"/>
</wpftk:DataGrid.Columns>
</wpftk:DataGrid>
明らかに、バインディングのプロパティはの有効なプロパティではありませんDataGridTemplateColumn
が、おそらくデータコンテキストをいじって、いくつかの相対的なソースでうまくいくかもしれませんが、率直に言って、それを実装する方法が見つかりません。
私が望んでいることが可能かどうかわからないので、「そんなことはできません」という回答を喜んで受け入れます
注:テンプレートのTextBlock
/TextBox
はテスト用です(実際のテンプレートははるかに複雑です)DataGridTextColumn
、うまくいきません よろしくお願いします