如何重用WPF DataGridTemplateColumn(包括绑定) [英] How to reuse WPF DataGridTemplateColumn (including binding)

查看:113
本文介绍了如何重用WPF DataGridTemplateColumn(包括绑定)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WPF数据网格中,我有一个定义为DataGridTemplateColumn的列,需要在各种列上使用。作为一个非常简化的示例,请考虑以下内容作为虚拟示例:

In WPF datagrids, I have a column defined as DataGridTemplateColumn which I'll need to be using on all kinds of columns. As a very simplified example please consider the below as a dummy sample:

<DataGrid ItemsSource="{Binding Path=ItemList, Mode=OneWay}" AutoGenerateColumns="False" >                
    <DataGrid.Columns>

        <DataGridTemplateColumn Header="Name" MinWidth="130" Width="Auto">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <DockPanel LastChildFill="True">
                        <Image Source="component/Images/test.png"/>
                        <TextBlock Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnDataErrors=True}"/>
                    </DockPanel>                                
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
            <DataGridTemplateColumn.CellEditingTemplate>
                <DataTemplate>
                    <DockPanel LastChildFill="True">
                        <Image Source="component/Images/test.png"/>
                        <TextBox Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnDataErrors=True}"/>
                    </DockPanel>
                </DataTemplate>                            
            </DataGridTemplateColumn.CellEditingTemplate>
        </DataGridTemplateColumn>

        <DataGridTextColumn Header="Company" Binding="{Binding Company, ValidatesOnDataErrors=True}" MinWidth="115" Width="Auto"/>                    
    </DataGrid.Columns>
</DataGrid>

举一个简单的例子,我如何将与Header = Name相同的模板应用于列

For a simple example, how could I apply the same template used for column with Header=Name to the column with Header=Company without having to reproduce the entire template for every column?

我找到了上一个SO问题,他们在其中使用以下资源进行解释:

I've found an answer with this previous SO question, where they explain using resources like:

<Application.Resources> 
     <DataTemplate x:Key="CellTemplate"> 
     ... 
     </DataTemplate> 
     <DataTemplate x:Key="CellEdintingTemplate"> 
     ... 
     </DataTemplate> 
</Application.Resources> 

<DataGrid Style="{StaticResource MainGridStyle}"> 
    <DataGrid.Columns> 
        <DataGridTemplateColumn CellTemplate="{StaticResource MyFirstColumnCellTemplate}" CellEdintingTemplate="{StaticResource MyFirstColumnCellEdintingTemplate}"/> 
        ... 
    </DataGrid.Columns> 
<DataGrid>  

那可以让我达到95%的水平,但是我最后缺少的是如何处理数据捆绑?如何在模板中创建某种类型的占位符,然后在网格中进行实际绑定?

That gets me 95% there, but the final piece I'm missing is how to handle the data binding? How do I create some type of place holder in the template and then do the actual binding in the grid?

EDIT
我一直在寻找并发现问题 创建通用DataGridTemplateColumn 听起来像我现在想做的事情实际上是不可能的。因此,如果其他任何人都试图这样做,并且看到这个问题,我不能保证这是不可能的,但是从这个链接看来,可能是这样。因此,只需为每列复制所有临时代码。

EDIT I've kept looking and found the question Create Common DataGridTemplateColumn which sounds like what I want to do may in fact be impossible currently. So if anyone else is ever trying to do this, and sees this question I cannot guarantee it is impossible, but from this link seems it may be. So will just need to duplicate all the tempalte code for every column.

推荐答案

您可以设置 CellStyle 属性,该样式将覆盖 DataGridCell 模板

You can set the CellStyle property to a style that overwrites the Template for the DataGridCell.

模板中,使用 ContentPresenter 绑定到 TemplatedParent.Content 的任何位置,因为 TemplatedParent DataGridCell

In the Template, use a ContentPresenter that is bound to the TemplatedParent.Content wherever you want to place the DataGridCell's Content, since the TemplatedParent is the DataGridCell

例如,

<Style x:Key="MyCellStyle" TargetType="{x:Type DataGridCell}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <Image Source="component/Images/test.png"/>
                    <ContentPresenter Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}" />
                </DockPanel>  
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<DataGrid ItemsSource="{Binding ItemList}" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" Binding="{Binding Name}" CellStyle="{StaticResource MyCellStyle}" MinWidth="130" Width="Auto" />
        <DataGridTextColumn Header="Company" Binding="{Binding Company}" CellStyle="{StaticResource MyCellStyle}" MinWidth="115" Width="Auto"/>                    
    </DataGrid.Columns>
</DataGrid>

这篇关于如何重用WPF DataGridTemplateColumn(包括绑定)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆