首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C# - WPF在DataGridColumnHeader中使用DataTrigger

C# - WPF在DataGridColumnHeader中使用DataTrigger
EN

Stack Overflow用户
提问于 2013-01-18 17:21:59
回答 1查看 2.4K关注 0票数 1

如何在DataGridColumHeader中使用DataTriggers?使用下面的代码,它不能工作。有人知道我怎么解决这个问题吗?

代码语言:javascript
运行
复制
<DataGridTextColumn.HeaderStyle>
   <Style TargetType="{x:Type DataGridColumnHeader}">
     <Setter Property="Background" Value="#FFFFBD21" />
     <Style.Triggers>
      <DataTrigger Binding="{Binding HasChangedRows}" Value="false">
        <Setter Property="Background" Value="#66FFBD21"/>
      </DataTrigger>
    </Style.Triggers>
  </Style>
</DataGridTextColumn.HeaderStyle>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-01-21 20:07:38

在新的解决方案中检查此代码,您将看到,如果正确设置了HasChangedRows绑定,则它应该可以工作。您可以使用复选框来更改HasChangedRows的值

代码语言:javascript
运行
复制
<Window x:Class="WpfApplication10.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
<StackPanel>
    <CheckBox IsThreeState="False" IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=HasChanges, Mode=TwoWay}"
              Content="{Binding RelativeSource={RelativeSource Mode=Self}, Path=IsChecked}"/>

    <DataGrid Name="MainGrid" AutoGenerateColumns="False">
        <DataGrid.Resources>
            <Style TargetType="{x:Type DataGridColumnHeader}" x:Key="customheaderstyle">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=HasChanges}" Value="false">
                        <Setter Property="Background" Value="#66FFBD21"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}, Path=HasChanges}" Value="true">
                        <Setter Property="Background" Value="#FFFFBD21" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn HeaderStyle="{StaticResource customheaderstyle}" Binding="{Binding}"/>
        </DataGrid.Columns>
    </DataGrid>
</StackPanel>
</Window>

和代码背后:

代码语言:javascript
运行
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;

namespace WpfApplication10
{
  public partial class MainWindow : Window, INotifyPropertyChanged
  {

   #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChange(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    #endregion


    private bool _HasChanges = false;
    public bool HasChanges
    {
        get { return this._HasChanges; }
        set
        {
            this._HasChanges = value;                
            NotifyPropertyChange("HasChanges");

        }
    } 

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        String[] list = { "1", "2", "3", "4" };
        this.MainGrid.ItemsSource = list;            
    }
  }
}

最终结果:

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14395781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档