.net – 如何在Silverlight数据网格中删除鼠标悬停突出显示和选定的行突出显示

.net – 如何在Silverlight数据网格中删除鼠标悬停突出显示和选定的行突出显示,第1张

概述我有一个datagrid,每行有一个按钮(xaml如下所示).我正试图摆脱突出显示所选行的蓝色突出显示功能,以及 用鼠标划过它.我正在尝试将其设置为只需单击按钮而不会获得行选择和鼠标悬停突出显示功能.我尝试将IsHitTestVisible设置为false,但是按钮不可单击.我怎样才能做到这一点? <data:DataGrid x:Name="grdClinics" 我有一个datagrID,每行有一个按钮(xaml如下所示).我正试图摆脱突出显示所选行的蓝色突出显示功能,以及
用鼠标划过它.我正在尝试将其设置为只需单击按钮而不会获得行选择和鼠标悬停突出显示功能.我尝试将IsHitTestVisible设置为false,但是按钮不可单击.我怎样才能做到这一点?

<data:DataGrID x:name="GrdClinics"               HorizontalAlignment="left"                VerticalAlignment="Bottom"                autoGenerateColumns="False"               headersVisibility="None"               RowHeight="55"               Background="transparent"               AlternatingRowBackground="transparent"               RowBackground="transparent"               borderBrush="transparent"               Foreground="transparent"                GrIDlinesVisibility="None"                SelectionMode="Single">                             <data:DataGrID.Columns>        <data:DataGrIDTemplateColumn header="Clinic">            <data:DataGrIDTemplateColumn.CellTemplate>                <DataTemplate>                    <button x:name="btnClinic"                             Height="46"                             WIDth="580"                             Content="{Binding Path=Description}"                             Style="{StaticResource Shinybutton}"                             Click="btnClinic_OnClick"                            FontSize="24"                            FontFamily="Tahoma"                            FontWeight="Bold">                        <button.Background>                            <linearGradIEntBrush EndPoint="0.528,1.144" StartPoint="1.066,1.221">                                <GradIEntStop color="#FF000000"/>                                <GradIEntStop color="#FFEDC88F" Offset="1"/>                            </linearGradIEntBrush>                        </button.Background>                    </button>                </DataTemplate>            </data:DataGrIDTemplateColumn.CellTemplate>        </data:DataGrIDTemplateColumn>    </data:DataGrID.Columns></data:DataGrID>
解决方法 简短的回答是使用样式.答案如下:

Silverlight 2.0数据网格中有两个样式属性可以解决您的问题.第一个是CellStyle,第二个是RowStyle. CellStyle属性将删除当前所选单元格周围的浅蓝色突出显示. RowStyle属性是您可以删除指示所选行的浅蓝色阴影的属性.我使用的CellStyle如下:

<Style x:Key="CellStyle" targettype="local:DataGrIDCell">        <Setter Property="Background" Value="transparent" />        <Setter Property="HorizontalContentAlignment" Value="Stretch" />        <Setter Property="VerticalContentAlignment" Value="Stretch" />        <Setter Property="Cursor" Value="Arrow" />        <Setter Property="IsTabStop" Value="False" />        <Setter Property="Template">            <Setter.Value>                <ControlTemplate targettype="local:DataGrIDCell">                    <GrID name="Root" Background="transparent">                        <vsm:visualstatemanager.VisualStateGroups>                            <vsm:VisualStateGroup x:name="CurrentStates" >                                <vsm:VisualStateGroup.Transitions>                                    <vsm:VisualTransition GeneratedDuration="0" />                                </vsm:VisualStateGroup.Transitions>                                <vsm:VisualState x:name="Regular" />                                <vsm:VisualState x:name="Current" />                                    <!--<Storyboard>                                        <DoubleAnimation Storyboard.Targetname="FocusVisual" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />                                    </Storyboard>                                </vsm:VisualState>-->                            </vsm:VisualStateGroup>                        </vsm:visualstatemanager.VisualStateGroups>                        <GrID.ColumnDeFinitions>                            <ColumnDeFinition WIDth="*" />                            <ColumnDeFinition WIDth="auto" />                        </GrID.ColumnDeFinitions>                        <Rectangle name="FocusVisual" stroke="#FF6DBDD1" strokeThickness="1" Fill="#66FFFFFF" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsHitTestVisible="false" Opacity="0" />                        <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" margin="{TemplateBinding padding}" />                        <Rectangle name="RightGrIDline" GrID.Column="1" VerticalAlignment="Stretch" WIDth="1" />                    </GrID>                </ControlTemplate>            </Setter.Value>        </Setter>    </Style>

如果您注意到,我注释掉了改变了FocusVisual矩形的不透明度值的故事板.这样做是为了将FocusVisual矩形设置为在单元格选择上显示. (请注意:您无法删除FocusVisual元素,因为CellPresenter需要此元素,而找不到该元素将导致错误.)

我使用的RowStyle如下:

<Style targettype="local:DataGrIDRow" x:Key="MyCustomrow">        <Setter Property="IsTabStop" Value="False" />        <Setter Property="Template">            <Setter.Value>                <ControlTemplate targettype="local:DataGrIDRow">                    <localprimitives:DataGrIDFroZenGrID x:name="Root">                        <localprimitives:DataGrIDFroZenGrID.Resources>                            <Storyboard x:Key="DetailsVisibleTransition" >                                <DoubleAnimation Storyboard.Targetname="DetailsPresenter" Storyboard.TargetProperty="ContentHeight" Duration="00:00:0.1" />                            </Storyboard>                        </localprimitives:DataGrIDFroZenGrID.Resources>                        <vsm:visualstatemanager.VisualStateGroups>                            <vsm:VisualStateGroup x:name="CommonStates" >                                <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="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1" />                                    </Storyboard>                                </vsm:VisualState>-->                                <vsm:VisualState x:name="MouSEOver Selected"/>                                <!--<Storyboard>                                        <DoubleAnimation Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1" />                                    </Storyboard>                                </vsm:VisualState>-->                                <vsm:VisualState x:name="Unfocused Selected"/>                                <!--<Storyboard>                                        <DoubleAnimation Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1" />                                        <colorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolIDcolorBrush.color)">                                            <SplinecolorKeyFrame KeyTime="0" Value="#FFE1E7EC" />                                        </colorAnimationUsingKeyFrames>                                    </Storyboard>                                </vsm:VisualState>-->                            </vsm:VisualStateGroup>                        </vsm:visualstatemanager.VisualStateGroups>                        <localprimitives:DataGrIDFroZenGrID.RowDeFinitions>                            <RowDeFinition Height="*" />                            <RowDeFinition Height="auto" />                            <RowDeFinition Height="auto" />                        </localprimitives:DataGrIDFroZenGrID.RowDeFinitions>                        <localprimitives:DataGrIDFroZenGrID.ColumnDeFinitions>                            <ColumnDeFinition WIDth="auto" />                            <ColumnDeFinition WIDth="*" />                        </localprimitives:DataGrIDFroZenGrID.ColumnDeFinitions>                        <Rectangle x:name="BackgroundRectangle" GrID.rowspan="2" GrID.ColumnSpan="2" Opacity="0" Fill="#FFBADDE9"  />                        <localprimitives:DataGrIDRowheader GrID.rowspan="3" x:name="Rowheader" localprimitives:DataGrIDFroZenGrID.IsFroZen="True" />                        <localprimitives:DataGrIDCellsPresenter x:name="CellsPresenter" localprimitives:DataGrIDFroZenGrID.IsFroZen="True"/>                        <localprimitives:DataGrIDDetailsPresenter GrID.Row="1" GrID.Column="1" x:name="DetailsPresenter" />                        <Rectangle GrID.Row="2" GrID.Column="1" x:name="BottomGrIDline" HorizontalAlignment="Stretch" Height="1" />                    </localprimitives:DataGrIDFroZenGrID>                </ControlTemplate>            </Setter.Value>        </Setter>    </Style>

如你所见,我评论了一些更多的视觉状态.您需要注释掉MouSEOver VisualState故事板,normal Selected故事板,MouSEOver Selected故事板和Unfocused Selected故事板.

(请注意:我没有删除这些视觉状态,我只是评论了他们过去做过的事情.)

这是我的完整代码供参考:(首先是XAML,然后是VB)

XAML:

<UserControl x:Class="DataGrID_MouSEOver.Page"    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:data="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Data"      xmlns:local="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Data"    xmlns:controls="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Data"    xmlns:primitives="clr-namespace:System.windows.Controls.Primitives;assembly=System.windows"    xmlns:localprimitives="clr-namespace:System.windows.Controls.Primitives;assembly=System.windows.Controls.Data"    xmlns:vsm="clr-namespace:System.windows;assembly=System.windows"><UserControl.Resources>    <Style x:Key="CellStyle" targettype="local:DataGrIDCell">        <!-- Todo: Remove this workaround to force MouseleftbuttonDown event to be raised when root element is clicked. -->        <Setter Property="Background" Value="transparent" />        <Setter Property="HorizontalContentAlignment" Value="Stretch" />        <Setter Property="VerticalContentAlignment" Value="Stretch" />        <Setter Property="Cursor" Value="Arrow" />        <Setter Property="IsTabStop" Value="False" />        <Setter Property="Template">            <Setter.Value>                <ControlTemplate targettype="local:DataGrIDCell">                    <GrID name="Root" Background="transparent">                        <vsm:visualstatemanager.VisualStateGroups>                            <vsm:VisualStateGroup x:name="CurrentStates" >                                <vsm:VisualStateGroup.Transitions>                                    <vsm:VisualTransition GeneratedDuration="0" />                                </vsm:VisualStateGroup.Transitions>                                <vsm:VisualState x:name="Regular" />                                <vsm:VisualState x:name="Current" />                                    <!--<Storyboard>                                        <DoubleAnimation Storyboard.Targetname="FocusVisual" Storyboard.TargetProperty="Opacity" To="1" Duration="0" />                                    </Storyboard>                                </vsm:VisualState>-->                            </vsm:VisualStateGroup>                        </vsm:visualstatemanager.VisualStateGroups>                        <GrID.ColumnDeFinitions>                            <ColumnDeFinition WIDth="*" />                            <ColumnDeFinition WIDth="auto" />                        </GrID.ColumnDeFinitions>                        <!-- Todo Refactor this if SL ever gets a FocusVisualStyle on FrameworkElement -->                        <Rectangle name="FocusVisual" stroke="#FF6DBDD1" strokeThickness="1" Fill="#66FFFFFF" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" IsHitTestVisible="false" Opacity="0" />                        <ContentPresenter Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" Cursor="{TemplateBinding Cursor}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" margin="{TemplateBinding padding}" />                        <Rectangle name="RightGrIDline" GrID.Column="1" VerticalAlignment="Stretch" WIDth="1" />                    </GrID>                </ControlTemplate>            </Setter.Value>        </Setter>    </Style>    <Style targettype="local:DataGrIDRow" x:Key="MyCustomrow">        <Setter Property="IsTabStop" Value="False" />        <Setter Property="Template">            <Setter.Value>                <ControlTemplate targettype="local:DataGrIDRow">                    <localprimitives:DataGrIDFroZenGrID x:name="Root">                        <localprimitives:DataGrIDFroZenGrID.Resources>                            <Storyboard x:Key="DetailsVisibleTransition" >                                <DoubleAnimation Storyboard.Targetname="DetailsPresenter" Storyboard.TargetProperty="ContentHeight" Duration="00:00:0.1" />                            </Storyboard>                        </localprimitives:DataGrIDFroZenGrID.Resources>                        <vsm:visualstatemanager.VisualStateGroups>                            <vsm:VisualStateGroup x:name="CommonStates" >                                <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="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1" />                                    </Storyboard>                                </vsm:VisualState>-->                                <vsm:VisualState x:name="MouSEOver Selected"/>                                <!--<Storyboard>                                        <DoubleAnimation Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1" />                                    </Storyboard>                                </vsm:VisualState>-->                                <vsm:VisualState x:name="Unfocused Selected"/>                                <!--<Storyboard>                                        <DoubleAnimation Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="Opacity" Duration="0" To="1" />                                        <colorAnimationUsingKeyFrames BeginTime="0" Duration="0" Storyboard.Targetname="BackgroundRectangle" Storyboard.TargetProperty="(Shape.Fill).(SolIDcolorBrush.color)">                                            <SplinecolorKeyFrame KeyTime="0" Value="#FFE1E7EC" />                                        </colorAnimationUsingKeyFrames>                                    </Storyboard>                                </vsm:VisualState>-->                            </vsm:VisualStateGroup>                        </vsm:visualstatemanager.VisualStateGroups>                        <localprimitives:DataGrIDFroZenGrID.RowDeFinitions>                            <RowDeFinition Height="*" />                            <RowDeFinition Height="auto" />                            <RowDeFinition Height="auto" />                        </localprimitives:DataGrIDFroZenGrID.RowDeFinitions>                        <localprimitives:DataGrIDFroZenGrID.ColumnDeFinitions>                            <ColumnDeFinition WIDth="auto" />                            <ColumnDeFinition WIDth="*" />                        </localprimitives:DataGrIDFroZenGrID.ColumnDeFinitions>                        <Rectangle x:name="BackgroundRectangle" GrID.rowspan="2" GrID.ColumnSpan="2" Opacity="0" Fill="#FFBADDE9"  />                        <localprimitives:DataGrIDRowheader GrID.rowspan="3" x:name="Rowheader" localprimitives:DataGrIDFroZenGrID.IsFroZen="True" />                        <localprimitives:DataGrIDCellsPresenter x:name="CellsPresenter" localprimitives:DataGrIDFroZenGrID.IsFroZen="True"/>                        <localprimitives:DataGrIDDetailsPresenter GrID.Row="1" GrID.Column="1" x:name="DetailsPresenter" />                        <Rectangle GrID.Row="2" GrID.Column="1" x:name="BottomGrIDline" HorizontalAlignment="Stretch" Height="1" />                    </localprimitives:DataGrIDFroZenGrID>                </ControlTemplate>            </Setter.Value>        </Setter>    </Style></UserControl.Resources><GrID x:name="LayoutRoot" Background="White">    <local:DataGrID x:name="TestGrID"           HorizontalAlignment="left"            VerticalAlignment="Bottom"            autoGenerateColumns="False"           headersVisibility="None"           RowHeight="55"           Background="transparent"           AlternatingRowBackground="transparent"           RowBackground="transparent"           borderBrush="transparent"           Foreground="transparent"            GrIDlinesVisibility="None"            SelectionMode="Single"           CellStyle="{StaticResource CellStyle}"            RowStyle="{StaticResource MyCustomrow}">        <local:DataGrID.Columns>            <local:DataGrIDTemplateColumn header="Clinic">                <local:DataGrIDTemplateColumn.CellTemplate>                    <DataTemplate>                        <button x:name="btnClinic"                         Height="46"                         WIDth="580"                         Content="{Binding Path=Description}"                         Click="btnClinic_Click"                        FontSize="24"                        FontFamily="Tahoma"                        FontWeight="Bold">                            <button.Background>                                <linearGradIEntBrush EndPoint="0.528,1.221">                                    <GradIEntStop color="#FF000000"/>                                    <GradIEntStop color="#FFEDC88F" Offset="1"/>                                </linearGradIEntBrush>                            </button.Background>                        </button>                    </DataTemplate>                </local:DataGrIDTemplateColumn.CellTemplate>            </local:DataGrIDTemplateColumn>        </local:DataGrID.Columns>    </local:DataGrID></GrID></UserControl>

VB:

Partial Public Class Pageinherits UserControlPublic Sub New()    InitializeComponent()    Dim test As IList(Of String) = New List(Of String)    test.Add("test1")    test.Add("test1")    test.Add("test1")    test.Add("test1")    test.Add("test1")    test.Add("test1")    test.Add("test1")    test.Add("test1")    test.Add("test1")    test.Add("test1")    TestGrID.ItemsSource = testEnd SubPrivate Sub btnClinic_Click(ByVal sender As System.Object,ByVal e As System.windows.RoutedEventArgs)End SubEnd Class

希望这可以帮助.

谢谢,斯科特

总结

以上是内存溢出为你收集整理的.net – 如何在Silverlight数据网格中删除鼠标悬停突出显示和选定的行突出显示全部内容,希望文章能够帮你解决.net – 如何在Silverlight数据网格中删除鼠标悬停突出显示和选定的行突出显示所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1005387.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存