【发布时间】:2012-12-13 14:42:57
【问题描述】:
我对 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 上指定绑定?
如果有任何正确方向的指点,我将不胜感激,
谢谢!
【问题讨论】:
-
我认为它应该可以帮助你codeproject.com/Articles/43525/…
标签: wpf datagrid datatemplate datagridtemplatecolumn