【问题标题】:Highlight entire rows only in Silverlight DataGrid仅在 Silverlight DataGrid 中突出显示整行
【发布时间】:2009-06-26 18:25:40
【问题描述】:

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

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

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

【问题讨论】:

    标签: silverlight silverlight-2.0


    【解决方案1】:

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

    只需将粘贴的代码添加到您的 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】:

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

      【讨论】:

        【解决方案3】:

        您可以在这些示例中创建一个样式来突出显示和对齐我拥有的:

            <!-- 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 中的整行的任何人。

        【讨论】:

        • 您的建议有效,但我有一个问题。我为 DataGridCell 定义了一个通用触发器(例如,如果它是只读的,则设置不同的背景)。但是,如果我在没有触发器的情况下应用您的 RightAlignedCell 样式,则此通用触发器似乎不起作用。为您的风格添加触发器(就像您在此处所做的那样)效果很好。你能解释一下为什么吗?
        猜你喜欢
        • 1970-01-01
        • 2010-11-27
        • 2011-04-27
        • 1970-01-01
        • 2011-10-01
        • 2013-12-20
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多