2012-04-24 28 views
1

我想在鼠標懸停在datagrid列標題上時更改整個datagrid列的背景顏色。這是我使用的風格的代碼。WPF DataGridColumnHeader MouseOver - 將觸發器應用於DataGridCell元素

<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> 

請幫助。

感謝

回答

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> 

這裏是我的觀點模型

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; } 
} 
相關問題