当我将鼠标悬停在该datagrid列标题上时,我想更改整个datagrid列的背景颜色。这是我正在使用的样式的代码。
<Style x:Key="RhinoDataGridBaseStyle" TargetType="{x:Type ctrls:RhinoDataGrid}">
<Style.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="White"></Setter>
<Setter Property="BorderThickness" Value="3"></Setter>
<Setter Property="BorderBrush" Value="#4CB7FF"></Setter>
<Setter Property="Foreground" Value="Black"></Setter>
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="HorizontalContentAlignment" Value="Center" />
<Setter Property="Height" Value="26"></Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
?????????????????????? What Should I write here ??????????????????????????????
</Trigger>
</Style.Triggers>
</Style>
<Style TargetType="{x:Type DataGridRowHeader}">
<Setter Property="Width" Value="36"></Setter>
</Style>
</Style.Resources>
请帮忙。
由于
答案 0 :(得分:1)
它有点棘手,但您可以使用附加属性将单元格链接到其标题。 这是我前一段时间使用过的一些代码(请原谅,如果它很长)
以下是附加的属性文件
public static class GroupMessaging
{
private static readonly Dictionary<string, List<DependencyObject>> messageDictionary = new Dictionary<string, List<DependencyObject>>();
public static readonly DependencyProperty MessageKeyProperty = DependencyProperty.RegisterAttached("MessageKey", typeof(string), typeof(GroupMessaging), new PropertyMetadata(null, OnMessageKeyChanged));
public static void SetMessageKey(UIElement element, string value)
{
element.SetValue(MessageKeyProperty, value);
}
public static string GetMessageKey(UIElement element)
{
return (string)element.GetValue(MessageKeyProperty);
}
public static readonly DependencyProperty MessageProperty = DependencyProperty.RegisterAttached("Message", typeof(string), typeof(GroupMessaging), new PropertyMetadata(null, OnMessageChanged));
public static void SetMessage(UIElement element, string value)
{
element.SetValue(MessageProperty, value);
}
public static string GetMessage(UIElement element)
{
return (string)element.GetValue(MessageProperty);
}
private static void OnMessageChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var key = d.GetValue(MessageKeyProperty);
if (key == null || !messageDictionary.ContainsKey((string)key))
{
return;
}
messageDictionary[(string)key].ForEach(o =>
{
var old = o.GetValue(MessageProperty);
if (o != d && e.NewValue != old)
{
o.SetValue(MessageProperty, e.NewValue);
}
});
}
private static void OnMessageKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.OldValue != null && messageDictionary.ContainsKey((string)e.OldValue))
{
messageDictionary[(string)e.OldValue].Remove(d);
}
if (e.NewValue != null)
{
if (!messageDictionary.ContainsKey((string)e.NewValue))
{
messageDictionary.Add((string)e.NewValue, new List<DependencyObject>());
}
messageDictionary[(string)e.NewValue].Add(d);
}
}
}
这是我的XAML
<DataGrid ItemsSource="{Binding Items}" AutoGenerateColumns="False">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="core:GroupMessaging.MessageKey" Value="{Binding RelativeSource={RelativeSource Mode=Self}, Path=Content}" />
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid x:Name="headergrid">
<TextBlock Text="{TemplateBinding Content}" />
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="core:GroupMessaging.Message" Value="Active" />
<Setter Property="Background" Value="Aqua" />
</Trigger>
</Style.Triggers>
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Header="Prop1">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid x:Name="templategrid" core:GroupMessaging.MessageKey="Prop1">
<TextBlock Text="{Binding Prop1}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=templategrid, Path=(core:GroupMessaging.Message)}" Value="Active">
<Setter TargetName="templategrid" Property="Background" Value="Aqua" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Prop2">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid x:Name="templategrid" core:GroupMessaging.MessageKey="Prop2">
<TextBlock Text="{Binding Prop2}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=templategrid, Path=(core:GroupMessaging.Message)}" Value="Active">
<Setter TargetName="templategrid" Property="Background" Value="Aqua" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Prop3">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid x:Name="templategrid" core:GroupMessaging.MessageKey="Prop3">
<TextBlock Text="{Binding Prop3}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=templategrid, Path=(core:GroupMessaging.Message)}" Value="Active">
<Setter TargetName="templategrid" Property="Background" Value="Aqua" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Prop4">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Grid x:Name="templategrid" core:GroupMessaging.MessageKey="Prop4">
<TextBlock Text="{Binding Prop4}" />
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding ElementName=templategrid, Path=(core:GroupMessaging.Message)}" Value="Active">
<Setter TargetName="templategrid" Property="Background" Value="Aqua" />
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
,这是我的viewmodel
public class MainViewModel
{
public List<Item> Items { get; private set; }
public MainViewModel()
{
Items = new List<Item>();
Items.Add(new Item() { Prop1 = "item1_1",
Prop2 = "item1_2", Prop3 = "item1_3", Prop4 = "item1_4"});
Items.Add(new Item() { Prop1 = "item2_1",
Prop2 = "item2_2", Prop3 = "item2_3", Prop4 = "item2_4"});
Items.Add(new Item() { Prop1 = "item3_1",
Prop2 = "item3_2", Prop3 = "item3_3", Prop4 = "item3_4"});
Items.Add(new Item() { Prop1 = "item4_1",
Prop2 = "item4_2", Prop3 = "item4_3", Prop4 = "item4_4"});
Items.Add(new Item() { Prop1 = "item5_1",
Prop2 = "item5_2", Prop3 = "item5_3", Prop4 = "item5_4"});
}
}
public class Item
{
public string Prop1 { get; set; }
public string Prop2 { get; set; }
public string Prop3 { get; set; }
public string Prop4 { get; set; }
}