C#-WPF在DataGridColumnHeader中使用DataTrigger [英] C# - WPF Using DataTrigger in DataGridColumnHeader
本文介绍了C#-WPF在DataGridColumnHeader中使用DataTrigger的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在DataGridColumHeader中使用DataTriggers?使用下面的这段代码将无法正常工作.有人知道我该如何解决?
How can I use DataTriggers in a DataGridColumHeader? With this code below it doesn't work. Someone got any idea how i can fix this?
<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>
推荐答案
在新的解决方案中检查此代码,您将看到,如果正确设置HasChangedRows Binding,它应该可以工作.您可以使用复选框更改HasChangedRows的值
Check This Code in a new solution and you will see that it should work if HasChangedRows Binding is correctly set. You can use the checkbox to change the value of HasChangedRows
<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>
和后面的代码:
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;
}
}
}
最终结果:
还有
这篇关于C#-WPF在DataGridColumnHeader中使用DataTrigger的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文