2

(Visual Studio 2010)从 web 示例中,我重写了ControlTemplateaDatagridTextColumn以包含绑定到 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>

感兴趣的线在底部附近:<Image Source="{Binding someProperty}" Grid.Column="1"/>。我希望能够ControlTemplate在应用程序的多个地方重用它。Column唯一的区别是它本身和这个添加的绑定路径Image。理想情况下,我可以声明 aTextColumn并声明此样式,并使用最少的附加代码指导绑定:

<DataGridTextColumn Binding="{Binding typicalProperty}"
                    CellStyle="myStyle" 
                    [this part can't do -->] ImageSourceBinding="{Binding anyPropertyInViewModel}" >

我遇到过多个用户的帖子,他们询问如何为使用不同绑定路径的列创建可重用的ControlTemplate, or TemplateColumn, or ,到目前为止,似乎没有人提供关于如何做到这一点的答案。一个常见的建议是尝试一个,但绑定不能正常工作。StyleDataGridAttachedProperty

肯定有更好的方法,而不是为每个单独的列复制/粘贴上述所有代码,然后更改绑定路径?在我看来,这将是完全荒谬的,并且完全违背了我所了解的关于编码冗余概念的所有内容......如果你的列数是几十个,甚至几百个怎么办?就目前而言,这是对可定制性的严重限制DataGrid......我必须在这里遗漏一些东西。

4

1 回答 1

1

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

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

    // value for TextBlock in DataGridCell
    public string AnotherTextValue 
    {
        get; 
        set;
    }
}

ObservableCollection这个类的:

public ObservableCollection<MyObject> Objects 
{
    get; 
    set; 
}

样式DataGridCell是可取的放置窗口的资源(Window.Resources),或App.xaml

<Window.Resources>
    <Style x:Key="MyCellStyle" TargetType="{x:Type DataGridCell}">
        ...
    </Style>
</Window.Resources>

TextBlockDataGridCell风格将如下所示:

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

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

定义DataGridTextColumn

<DataGridTextColumn CellStyle="{StaticResource MyCellStyle}" Binding="{Binding BaseValue}" Header="My Super Header" />
于 2013-08-07T17:21:13.813 回答