仅在Silverlight DataGrid中突出显示整行

时间:2009-06-26 18:25:41

标签: silverlight silverlight-2.0

当用户点击数据网格中的一行(或使用键盘)时,该行被选中,但他们点击的特定单元格也有自己的特殊焦点。这对于数据编辑网格来说很好,但我试图创建更像是一个打开的对话框,显示列表中每个项目的属性,所以......

是否可以配置(只读)DataGrid,以便用户只能选择或关注整个行,而不是单个字段。

如果无法做到这一点,是否有一种优雅的方法可以只选择第一个元素 - 例如在标准的Windows打开对话框中,如果更改为“详细信息”视图,则每行有多个列(文件名,创建日期,大小等),但您只能突出显示文件名列中的项目。

3 个答案:

答案 0 :(得分:8)

这是我的(跛脚)版本,在所选行上添加黑色背景,在当前行上添加灰色背景。我不得不覆盖样式,因为我是单独绘制单元格并且选中的行是隐藏的。

只需将粘贴的代码添加到DataGrid实例:

        <local:DataGrid.RowStyle>
            <Style TargetType="local:DataGridRow">
                <Setter Property="IsTabStop" Value="False" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="local:DataGridRow">
                            <localprimitives:DataGridFrozenGrid Name="Root">
                                <vsm:VisualStateManager.VisualStateGroups>
                                    <vsm:VisualStateGroup x:Name="CommonStates">
                                        <vsm:VisualStateGroup.Transitions>
                                            <vsm:VisualTransition GeneratedDuration="0" />
                                        </vsm:VisualStateGroup.Transitions>
                                        <vsm:VisualState x:Name="Normal" />
                                        <vsm:VisualState x:Name="Normal AlternatingRow">
                                            <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="Normal Selected">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangleSelected" Storyboard.TargetProperty="Opacity" Duration="0" To="1" />
                                            </Storyboard>
                                        </vsm:VisualState>
                                        <vsm:VisualState x:Name="MouseOver Selected">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangleSelected" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                                            </Storyboard>
                                        </vsm:VisualState>
                                        <vsm:VisualState x:Name="Unfocused Selected">
                                            <Storyboard>
                                                <DoubleAnimation Storyboard.TargetName="BackgroundRectangleSelected" Storyboard.TargetProperty="Opacity" Duration="0" To="1"/>
                                                <ColorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.TargetName="BackgroundRectangleSelected" Storyboard.TargetProperty="(Shape.Fill).(SolidColorBrush.Color)">
                                                    <SplineColorKeyFrame KeyTime="0" Value="Black"/>
                                                </ColorAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </vsm:VisualState>
                                    </vsm:VisualStateGroup>
                                </vsm:VisualStateManager.VisualStateGroups>
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="*"/>
                                    <RowDefinition Height="Auto"/>
                                    <RowDefinition Height="Auto"/>
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </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="#FFBADDE9"/>
                                <Rectangle x:Name="BackgroundRectangleSelected" Grid.RowSpan="2" Grid.ColumnSpan="2" Opacity="0" Fill="Black"/>

                                <localprimitives:DataGridRowHeader Grid.RowSpan="3" Name="RowHeader" localprimitives:DataGridFrozenGrid.IsFrozen="True" />
                                <localprimitives:DataGridCellsPresenter Margin="2" 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>
            </Style>
        </local:DataGrid.RowStyle>

答案 1 :(得分:1)

您可以更改DataGridCell的样式 这是一篇有用的文章 http://leeontech.wordpress.com/2009/06/29/hilighting-entire-rows-in-datagrid/

答案 2 :(得分:1)

你可以像这些样本一样创建一个样式来突出显示并对齐我:

    <!-- Right Aligned Cell -->
    <Style x:Key="RightAlignedCell" TargetType="{x:Type dg:DataGridCell}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type dg:DataGridCell}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Right" VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="dg:DataGridCell.IsSelected" Value="True">
                <Setter Property="Background" Value="#356815" />
                <Setter Property="Foreground" Value="#e2fce2" />
            </Trigger>
        </Style.Triggers>
    </Style>

    <!-- Center Aligned Cell -->
    <Style x:Key="CenterAlignedCell" TargetType="{x:Type dg:DataGridCell}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type dg:DataGridCell}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="dg:DataGridCell.IsSelected" Value="True">
                <Setter Property="Background" Value="#356815" />
                <Setter Property="Foreground" Value="#e2fce2" />
            </Trigger>
        </Style.Triggers>
    </Style>

后来你把:

<dg:DataGrid x:Name="dg" AutoGenerateColumns="False" 
            xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" ItemsSource="{Binding}" 
            SelectionMode="Single" ...>
    <dg:DataGridTextColumn Header="Total Amount" Width="110" CellStyle="{StaticResource RightAlignedCell}"
                                   Binding="{Binding Total,StringFormat={}\{0:N0\}}" IsReadOnly="True"/>
    <dg:DataGridTextColumn Header="Enter Date" Width="110" CellStyle="{StaticResource CenterAlignedCell}"
                                   Binding="{Binding EnterDate,StringFormat={}\{0:dd/MM/yyyy\}}" IsReadOnly="True"/>

...

我希望这适用于任何想要在WPF DataGrid中突出显示完整行的人。