wpf - 在WPF中创建Common DataGridTemplateColumn

标签 wpf xaml datagridtemplatecolumn

我需要创建一个通用的DataGridTemplateColumn,以便可以在具有不同对象和属性的应用程序中使用它。

这是一些示例代码,我在我的项目中使用

<DataGridTemplateColumn Width="100*">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Age}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>   
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox Text="{Binding Path=Age}"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>

我需要代码的通用版本,以便可以将DataTemplate放入app.xaml并在我的代码中引用它

最佳答案

您无法直接将DataGridTemplateColumn模板化。但幸运的是,您可以为单元使用全局模板。看一个例子:

App.xaml

<Application.Resources>

    <DataTemplate x:Key="CellEdintingTemplate">
        <TextBox Text="{Binding Path=Age}" />
    </DataTemplate>
    <DataTemplate x:Key="CellTemplate">
        <TextBlock Text="{Binding Path=Age}" />
    </DataTemplate>

</Application.Resources>

使用
    <DataGrid>
        <DataGrid.Columns>
            <DataGridTemplateColumn 
                 CellEditingTemplate="{StaticResource CellEdintingTemplate}" 
                 CellTemplate="{StaticResource CellTemplate}" />
        </DataGrid.Columns>
    </DataGrid>

关于wpf - 在WPF中创建Common DataGridTemplateColumn,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10361122/

相关文章:

wpf - 在 WPF 网格/列表中跨越多列的最佳方法?

xaml - 绑定(bind)到 ElementName 取决于绑定(bind)顺序

.net - Windows 10 ComboBox 的 WPF 样式

mvvm - 尝试编辑时 WPF DataGrid 上动态列的两种方式绑定(bind)异常

wpf - 在内置的WPF DataGrid中,我可以为DataGridTemplateColumn设置数据源吗?

c# - 防止触发 CheckBox Checked 事件

c# - IMul​​tiValueConverter - INotifyPropertyChanged

c# - 如何在 wpf ViewModel 中实现 observable int?

c# - 通过代码将 Eventhandler 添加到 DataTemplate 内的形状

WPF DataGridTemplateColumn 共享模板?