Silverlight DataGrid 行颜色绑定 [英] Silverlight DataGrid row color binding

查看:18
本文介绍了Silverlight DataGrid 行颜色绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到一种方法将 DataGrid 行的背景颜色绑定到绑定对象的属性.

I'd like to find a way to bind the background color of rows of a DataGrid to a property of my bound objects.

这是我的 XAML:

<sdk:DataGrid ItemsSource="{Binding MyItems}" />

我在 Silverlight 4 中使用 MVVM Light Toolkit.

I'm using the MVVM Light Toolkit with Silverlight 4.

推荐答案

您可以通过更改行模板来实现:

You can do it by changing a row template:

<sdk:DataGrid ItemsSource="{Binding MyItems}">
    <sdk:DataGrid.RowStyle>
        <Style TargetType="sdk:DataGridRow">
            <Setter Property="Template">
            ...
                <Rectangle x:Name="BackgroundRectangle" Fill="{Binding ColorPropertyOfItem}" />
            ...
            </Setter>
        </Style>
    </sdk:DataGrid.RowStyle>
</sdk:DataGrid>

或者您可以将每一列标记为 TemplateColumn 并在每列中明确设置背景颜色:

Or you can mark each column as TemplateColumn and set background color explicitly in each of them:

<sdk:DataGridTemplateColumn Header="ColumnName" SortMemberPath="ColumnProperty">
    <sdk:DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ColumnProperty}" Background="{Binding ColorPropertyOfItem}" />
        </DataTemplate>
    </sdk:DataGridTemplateColumn.CellTemplate>
</sdk:DataGridTemplateColumn>

这是DataGridRow 类的完整模板:

<Setter Property="Template">
    <Setter.Value>
        <ControlTemplate TargetType="local:DataGridRow" 
             xmlns:localprimitives="clr-namespace:System.Windows.Controls.Primitives;assembly=System.Windows.Controls.Data"
             xmlns:local="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data"
             xmlns:vsm="clr-namespace:System.Windows;assembly=System.Windows">
            <localprimitives:DataGridFrozenGrid Name="Root">
                <vsm:VisualStateManager.VisualStateGroups>
                    <vsm:VisualStateGroup x:Name="CommonStates">
                        <vsm:VisualState x:Name="Normal"/>
                    <vsm:VisualState x:Name="NormalAlternatingRow">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="0"/>
                            </Storyboard>
                        </vsm:VisualState>
                        <vsm:VisualState x:Name="MouseOver">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To=".5"/>
                            </Storyboard>
                        </vsm:VisualState>
                        <vsm:VisualState x:Name="NormalSelected">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                            </Storyboard>
                        </vsm:VisualState>
                        <vsm:VisualState x:Name="MouseOverSelected">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                            </Storyboard>
                        </vsm:VisualState>
                        <vsm:VisualState x:Name="UnfocusedSelected">
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                                <ColorAnimation Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="(Fill).Color" To="#FFE1E7EC"/>
                            </Storyboard>
                        </vsm:VisualState>
                    </vsm:VisualStateGroup>
                    <vsm:VisualStateGroup x:Name="ValidationStates">
                        <vsm:VisualState x:Name="Valid"/>
                        <vsm:VisualState x:Name="Invalid">
                            <Storyboard>
                                <ObjectAnimationUsingKeyFrames Duration="0" Storyboard.TargetName="BackgroundRectangle" Storyboard.TargetProperty="Visibility">
                                    <DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
                                </ObjectAnimationUsingKeyFrames>
                                <DoubleAnimation Storyboard.TargetName="InvalidVisualElement" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                            </Storyboard>
                        </vsm:VisualState>
                    </vsm:VisualStateGroup>
                </vsm:VisualStateManager.VisualStateGroups>
                <Grid.RowDefinitions>
                    <RowDefinition/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition/>
                </Grid.ColumnDefinitions>

                <Grid.Resources>
                    <Storyboard x:Key="DetailsVisibleTransition">
                        <DoubleAnimation Storyboard.TargetName="DetailsPresenter" Storyboard.TargetProperty="ContentHeight" Duration="00:00:0.1" />
                    </Storyboard>
                </Grid.Resources>

                <Rectangle x:Name="BackgroundRectangle" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="{Binding ColorPropertyOfItem}"/>
                <Rectangle x:Name="InvalidVisualElement" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="#FFF7D8DB"/>

                <localprimitives:DataGridRowHeader Grid.RowSpan="3" Name="RowHeader" localprimitives:DataGridFrozenGrid.IsFrozen="True" />
                <localprimitives:DataGridCellsPresenter Grid.Column="1" Name="CellsPresenter" localprimitives:DataGridFrozenGrid.IsFrozen="True" />
                <localprimitives:DataGridDetailsPresenter Grid.Row="1" Grid.Column="1" Name="DetailsPresenter" />
                <Rectangle Grid.Row="2" Grid.Column="1" Name="BottomGridLine" HorizontalAlignment="Stretch" Height="1" />
            </localprimitives:DataGridFrozenGrid>
        </ControlTemplate>
    </Setter.Value>
</Setter>

并且不要忘记将 Binding ColorPropertyOfItem 更改为模型中 Brush 类型的真实属性.

And don't forget to change Binding ColorPropertyOfItem to a real property of the type Brush in your model.

这篇关于Silverlight DataGrid 行颜色绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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