单击rowheader内的togglebutton时如何更改Datagrid行背景?
Posted
技术标签:
【中文标题】单击rowheader内的togglebutton时如何更改Datagrid行背景?【英文标题】:How to change Datagrid row background when clicking on togglebutton inside rowheader? 【发布时间】:2014-08-08 08:15:12 【问题描述】:我正在覆盖 DataGrid 行标题样式,但遇到以下两个问题:
1.DataGrid 行标题包含一个图像和一个切换按钮。当 ToggleButton 被“选中”时,我想更改整行的背景颜色,但是我只设法更改了行标题的背景,无法找到在行级别触发触发器的方法。
2.选择行时,行和标题背景设置为不同的背景,另一种方式不起作用。我需要做的是在选择行标题时更改行的背景
这是DataGridRowHeader的样式代码
<Style x:Key="x:Type DataGridRowHeader" TargetType="x:Type DataGridRowHeader">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="x:Type DataGridRowHeader">
<Grid>
<Border Name="RowHeaderBorder"
BorderThickness="0,0,3,0"
Margin="0,0,0,0"
BorderBrush="StaticResource DataGridRowBorder">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0" Source="Binding ImageSource" RenderOptions.BitmapScalingMode="HighQuality" Stretch="None" VerticalAlignment="Center" HorizontalAlignment="Center"/>
<ToggleButton x:Name="tglButton" Visibility="Binding ActiveCall" Grid.Column="1" Content="Button" Focusable="True" BorderThickness="1" Width="80" Height="33" VerticalAlignment="Top" >
</ToggleButton>
</Grid>
</Border>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Binding="Binding ElementName=tglButton, Path=IsChecked" Value="true">
<Setter Property="Background" TargetName="RowHeaderBorder" Value ="Green"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这是 DataGridRow 的样式:
<Style x:Key="DataGridRowStyle" TargetType="x:Type DataGridRow">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="StaticResource DataGridRowBorder"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="StaticResource DataGridRowHoveredBackground" />
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect ShadowDepth="0" BlurRadius="20"/>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background" Value="StaticResource RowBackgroundSelectedBrush2" />
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect ShadowDepth="0" BlurRadius="20"/>
</Setter.Value>
</Setter>
</Trigger>
<DataTrigger Binding="Binding ElementName=DataGridRowHeader.tglButton, Path=IsChecked" Value="true">
<Setter Property="Background" Value ="Green"/>
</DataTrigger>
</Style.Triggers>
</Style>
【问题讨论】:
在您的按钮触发器中,您尝试过这个<DataTrigger Binding="Binding ElementName=tglButton, Path=IsChecked" Value="true"> <Setter Property="DataGridRow.Background" Value ="Green"/> </DataTrigger>
@XAMlMAX,我已经尝试过这个触发器,但它也不起作用
【参考方案1】:
这是因为tglButton
存在于行标题中,它是DataGridRow
的子项。子行标题中定义的触发器正在尝试更新父 (DataGridRow
) 的属性并且无法找到它。
对此的一种解决方案是在DataGridRow
的模板内定义DataGridRowHeader
的模板并为其提供一个名称,该名称可在触发器中使用。一个非常粗略的例子:
<Style
TargetType="x:Type DataGridRow">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="x:Type DataGridRow">
<Border x:Name="DGR_Border"
BorderBrush="TemplateBinding BorderBrush"
BorderThickness="TemplateBinding BorderThickness"
Background="TemplateBinding Background"
SnapsToDevicePixels="True"
CornerRadius="8,8,8,8">
<SelectiveScrollingGrid>
<SelectiveScrollingGrid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</SelectiveScrollingGrid.ColumnDefinitions>
<SelectiveScrollingGrid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</SelectiveScrollingGrid.RowDefinitions>
<DataGridCellsPresenter Grid.Column="1"
ItemsPanel="TemplateBinding ItemsPanel"
SnapsToDevicePixels="TemplateBinding SnapsToDevicePixels" />
<DataGridDetailsPresenter Grid.Column="1"
Grid.Row="1"
SelectiveScrollingGrid.SelectiveScrollingOrientation="Binding AreRowDetailsFrozen,
ConverterParameter=x:Static SelectiveScrollingOrientation.Vertical,
Converter=x:Static DataGrid.RowDetailsScrollingConverter,
RelativeSource=RelativeSource AncestorType=x:Type DataGrid"
Visibility="TemplateBinding DetailsVisibility" />
<DataGridRowHeader Name="RHeader" Grid.RowSpan="2"
SelectiveScrollingGrid.SelectiveScrollingOrientation="Vertical"
Visibility="Binding HeadersVisibility,
ConverterParameter=x:Static DataGridHeadersVisibility.Row,
Converter=x:Static DataGrid.HeadersVisibilityConverter,
RelativeSource=RelativeSource AncestorType=x:Type DataGrid">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="50" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ToggleButton x:Name="tglButton"
Grid.Column="1"
Content="Button"
Focusable="True"
BorderThickness="1"
Width="80"
Height="33"
VerticalAlignment="Top">
</ToggleButton>
</Grid>
</DataGridRowHeader>
</SelectiveScrollingGrid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected"
Value="True">
<Setter TargetName="DGR_Border"
Property="Background"
Value="Gray" />
</Trigger>
<DataTrigger Binding="Binding ElementName=tglButton, Path=IsChecked"
Value="true">
<Setter Property="Background"
TargetName="RHeader"
Value="Green" />
<Setter Property="Background"
Value="Green" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="DataContext"
Value="Binding RelativeSource=RelativeSource Self" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="BorderBrush"
Value="Aqua" />
<Style.Triggers>
<Trigger Property="IsSelected"
Value="true">
<Setter Property="Foreground"
Value="Black" />
<Setter Property="Background"
Value="Pink" />
<Setter Property="FontWeight"
Value="Bold" />
<Setter Property="Effect">
<Setter.Value>
<DropShadowEffect ShadowDepth="0"
BlurRadius="20" />
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
【讨论】:
以上是关于单击rowheader内的togglebutton时如何更改Datagrid行背景?的主要内容,如果未能解决你的问题,请参考以下文章
阻止UserForm_Initialize触发ToggleButton_Click事件
如何使用RowHeader处理JTable中的RowSorter同步?
如何通过单击功能区按钮将文本设置到自定义任务窗格中的文本框?
如何在 DragAndDrop 期间在 Main-JTable 上绘制 RowHeader-JTable 的 Dropline?