私はWPFを初めて使用するので、これが明らかな場合は申し訳ありませんが、インターネット上でそれがどのように行われるかを示す適切な例を見つけることができないようです。
MyCollectionというDataItemのコレクションにバインドされているDataGridがあります。グリッド内の複数の列(および必要に応じてアプリケーションの他の場所)に使用できる汎用DataTemplateを作成したいと思います。
例えば
<DataGrid ItemsSource="{Binding MyCollection}" AutoGenerateColumns="False" SelectionUnit="Cell" EnableColumnVirtualization="True">
<DataGrid.Columns>
<DataGridTemplateColumn Header="File path" CellTemplate="{StaticResource FileSelectorEditorTemplate}" CellEditingTemplate="{StaticResource FileSelectorEditorTemplate}" />
<DataGridTemplateColumn Header="File path2" CellTemplate="{StaticResource FileSelectorEditorTemplate}" CellEditingTemplate="{StaticResource FileSelectorEditorTemplate}" />
<DataGridTemplateColumn Header="File path3" CellTemplate="{StaticResource FileSelectorEditorTemplate}" CellEditingTemplate="{StaticResource FileSelectorEditorTemplate}" />
...
私のDataTemplateは、現在、アプリケーションリソースで次のように定義されています。
<DataTemplate x:Key="FileSelectorEditorTemplate">
<Grid>
<TextBox Text="{Binding FilePath.PhysicalPath}" HorizontalAlignment="Stretch" Margin="0,0,35,0" />
<Button Content="..." Height="25" Width="25" Margin="0,0,5,0" HorizontalAlignment="Right" Click="FileOpen_Click" />
</Grid>
</DataTemplate>
ここで問題となるのは、バインディングがDataTemplateで指定されているのに対し、ビューモデルのプロパティFilePath、FilePath2、FilePath3ごとに異なるバインディングを適用する必要があることです。DataGridTemplateColumnでBindingを指定できないようですが?
正しい方向へのポインタをいただければ幸いです。
ありがとう!