本文介绍了WPF重用DataGrid TemplateColumn DataTemplates的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立了一个具有自定义列的数据网格:

I've build a datagrid which has a custom column:

<DataGridTemplateColumn
    Header="{x:Static local:MainWindowResources.gasNameLabel}"
    Width="*"
    MinWidth="150">
    <DataGridTemplateColumn.CellEditingTemplate>
        <DataTemplate>
            <TextBox
                Name="GasNameTextBox"
                Text="{Binding Path=Name, UpdateSourceTrigger=PropertyChanged}"
                Padding="2,0,0,0" />
            <DataTemplate.Triggers>
                <Trigger
                    SourceName="GasNameTextBox"
                    Property="IsVisible"
                    Value="True">
                    <Setter
                        TargetName="GasNameTextBox"
                        Property="FocusManager.FocusedElement"
                        Value="{Binding ElementName=GasNameTextBox}"/>
                </Trigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </DataGridTemplateColumn.CellEditingTemplate>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Label
                Name="GasNameLabel"
                Content="{Binding Path=Name}"
                Padding="0,0,0,0"
                Margin="6,2,2,2" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

由于我将重用此类列定义A LOT,因此我真的很想将其定义为一个外部DataTemplate,我只提供要绑定的属性(Binding Path = ...),其余的是重用...那样,我将定义文本模板,复选框模板等,并在各种网格中重用它们,仅将绑定更改为不同的属性.

As I am going to be reusing such column definition A LOT, I would really like to define it as an external DataTemplate to which I only provide the property to bind on (Binding Path= ...) and that the rest is reused...that way I would define Text template, Checkbox template and such and reuse them in various grids and only change bindings to different properties.

有可能吗?

弗拉丹

推荐答案

使用 UserControl ,而是将其放入应用程序资源中,然后重新使用usercontrol.

Use a UserControl instead, put that in your application resources, and reuse the usercontrol.

这篇关于WPF重用DataGrid TemplateColumn DataTemplates的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 02:59