微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

仅在Silverlight DataGrid中突出显示整行

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

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

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

解决方法

这是我的(跛脚)版本,在当前行的所选行和灰色背景上添加一个黑色背景.我必须覆盖样式,因为我单独绘制单元格,并且所选择的行被隐藏.

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐