代码之家  ›  专栏  ›  技术社区  ›  David

仅在Silverlight DataGrid中高亮显示整行

  •  12
  • David  · 技术社区  · 15 年前

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

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

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

    3 回复  |  直到 15 年前
        1
  •  8
  •   R4cOOn    15 年前

    这是我的(lame)版本,它在所选行上添加黑色背景,在当前行上添加灰色背景。我必须覆盖样式,因为我正在单独绘制单元格,而选定的行被隐藏。

    只需将粘贴的代码添加到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>
    
        2
  •  1
  •   user122069    15 年前

    您可以更改DataGridCell的样式 这里有一个帖子会有帮助 http://leeontech.wordpress.com/2009/06/29/hilighting-entire-rows-in-datagrid/

        3
  •  1
  •   Junior Mayhé    15 年前

    您可以在以下示例中创建样式以高亮显示和对齐我的:

        <!-- 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中的整行的人有用。