1

そのデータグリッド列ヘッダーにマウスを合わせると、そのデータグリッド列全体の背景色を変更したいと思います。これが私が使用しているスタイルのコードです。

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

助けてください。

ありがとう

4

1 に答える 1

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; }
}
于 2012-04-26T15:59:10.107 に答える