Wpf 不同绑定源需要可重用的DataCell ControlTemplate

Wpf 不同绑定源需要可重用的DataCell ControlTemplate,wpf,binding,datagrid,styles,Wpf,Binding,Datagrid,Styles,(Visual Studio 2010)在web示例中,我覆盖了DatagridTextColumn的ControlTemplate,以包含绑定到ViewModel的附加图像 <DataGridTextColumn.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="Template"> <Setter.Value>

(Visual Studio 2010)在web示例中,我覆盖了
DatagridTextColumn
ControlTemplate
,以包含绑定到ViewModel的附加图像

   <DataGridTextColumn.CellStyle>
    <Style TargetType="DataGridCell">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="DataGridCell">
                    <Grid  Name="Root" Background="{TemplateBinding Background}">

                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CurrentStates">
                                <VisualState x:Name="Regular" />
                                <VisualState x:Name="Current">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="FocusVisual" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                            <VisualStateGroup x:Name="ValidationStates">
                                <VisualState x:Name="Valid"/>
                                <VisualState x:Name="Invalid">
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetName="InvalidVisualElement" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                                        <ColorAnimation Storyboard.TargetName="FocusVisual" Storyboard.TargetProperty="(Fill).Color" Duration="0" To="#FFFFFFFF"/>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>


                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>

                        <Rectangle Name="FocusVisual" Stroke="#FF6DBDD1" StrokeThickness="1" Fill="#66FFFFFF" HorizontalAlignment="Stretch" 
                           VerticalAlignment="Stretch" IsHitTestVisible="false" Opacity="0" />

                        <ContentPresenter ContentSource="Content" />

                        <Image Source="{Binding someProperty}" Grid.Column="1"/>

                        <Rectangle x:Name="InvalidVisualElement" IsHitTestVisible="False" StrokeThickness="1" Stroke="#FFDC000C" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Opacity="0"/>
                        <Rectangle Name="RightGridLine" Grid.Column="1" VerticalAlignment="Stretch" Width="1" />

                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGridTextColumn.CellStyle>
我遇到过多篇用户帖子,询问如何使用不同的绑定路径为
DataGrid
的列创建可重用的
ControlTemplate
,或
TemplateColumn
,或
Style
,到目前为止,似乎还没有人给出如何做到这一点的答案。一个常见的建议是尝试
AttachedProperty
,但绑定不正常


当然,必须有一种更好的方法,而不是为每个单独的列复制/粘贴所有上述代码,然后更改绑定路径?在我看来,这完全是荒谬的,完全违背了我所学到的关于编码冗余的概念。。。如果你的列数是几十个,甚至几百个呢?目前,这严重限制了数据网格的可定制性。。。我在这里肯定遗漏了什么。

如果我理解正确,您需要添加数据类属性,例如-
另一个TextValue
。例如,我们有一个类
MyObject

public class MyObject
{
    public string BaseValue
    {
        get; 
        set; 
    }

    // value for TextBlock in DataGridCell
    public string AnotherTextValue 
    {
        get; 
        set;
    }
}
observedcollection
此类别的:

public ObservableCollection<MyObject> Objects 
{
    get; 
    set; 
}
DataGridCell
样式中的
TextBlock
如下所示:

...
<ContentPresenter ContentSource="Content" />

<TextBlock Text="{Binding AnotherTextValue}" Grid.Column="1" />

是的,我想你了解基本的想法。但是如果我在datagrid中有很多这种样式的列,但是每列的
TextBlock
都有一个特定的绑定路径(不总是
另一个textValue
)。有没有一种方法可以反复使用
MyCellStyle
,只使用
TextBlock
的不同绑定路径?@Tekito:是的,我理解。我正试图通过使用附加的依赖属性来实现此行为,但我还没有工作。如果成功了,我会告诉你的。谢谢。顺便说一句,我还尝试在列上创建附加属性,但只有在提供文字值时才起作用。我无法将附加属性绑定到视图模型的属性。但我是WPF的新手,所以也许我做错了什么这是指向使用附加属性的早期尝试的链接。我还尝试使用嵌套对象:每一列都绑定到一个具有
子值
属性的对象,因此主单元格文本绑定到
,附加的
文本块
绑定到
子值
。但不管是否嵌套,我都无法让它工作。@Tekito:我试图在C#上创建它的附加依赖属性,但没有成功。我会试着描述我的过程(只想说,我可能错了)。我的属性已成功创建,并通过
DataGridTextColumn
获取值,但当我尝试通过
TemplateBinding
(或其他方法)在
DataGridCell
样式中设置此值时,意味着不会传输任何内容,尽管已成功设置属性的值。也许,在
DataGridCell
的样式中,只能是
DataContext
的值。目前,我可以建议使用我的示例中的方法。
<Window.Resources>
    <Style x:Key="MyCellStyle" TargetType="{x:Type DataGridCell}">
        ...
    </Style>
</Window.Resources>
...
<ContentPresenter ContentSource="Content" />

<TextBlock Text="{Binding AnotherTextValue}" Grid.Column="1" />
<DataGridTextColumn CellStyle="{StaticResource MyCellStyle}" Binding="{Binding BaseValue}" Header="My Super Header" />