我的值为OperativeCount
。当这个数字大于10时,我希望DataGridColumn
的颜色发生变化。与此类似;
<DataGrid.Resources>
<Style x:Key="DGCellStyle" TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding OperativeCount}" Value=">10">
<Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
显然现在Value=">10"
不起作用,但基本上我想做的就是这样。
答案 0 :(得分:2)
适用于WPF的Blend SDK可以非常快速地完成它,而无需任何代码。查看DataTrigger (Blend SDK for WPF)。使用ChangePropertyAction作为行为。
<ei:DataTrigger Binding="{Binding OperativeCount}" Comparison="GreaterThan" Value="10">
<ei:ChangePropertyAction PropertyName="FontWeight" >
<ei:ChangePropertyAction.Value>
<FontWeight>Bold</FontWeight>
</ei:ChangePropertyAction.Value>
</ei:ChangePropertyAction>
</ei:DataTrigger>
不要太烦人,让Blend照顾它。
答案 1 :(得分:1)
为此您需要一个值转换器。如果你是我,那么你可以写下这个:
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;
namespace EdPlunkett
{
public class GreaterThan : MarkupExtension, IValueConverter
{
// The only public constructor is one that requires a double argument.
// Because of that, the XAML editor will put a blue squiggly on it if
// the argument is missing in the XAML.
public GreaterThan(double opnd)
{
Operand = opnd;
}
/// <summary>
/// Converter returns true if value is greater than this.
///
/// Don't let this be public, because it's required to be initialized
/// via the constructor.
/// </summary>
protected double Operand { get; set; }
// When the XAML is parsed, each markup extension is instantiated
// and the parser asks it to provide its value. Here, the value is
// us.
public override object ProvideValue(IServiceProvider serviceProvider)
{
return this;
}
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return System.Convert.ToDouble(value) > Operand;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
以下是您将如何使用它:
<Window
....
xmlns:edp="clr-namespace:EdPlunkett"
....
>
<DataGrid.Resources>
<Style x:Key="DGCellStyle" TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger
Binding="{Binding OperativeCount, Converter={edp:GreaterThan 10}}"
Value="True">
<Setter Property="FontWeight" Value="Bold" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Resources>
值转换器经常被实例化为资源,它们的参数通常通过ConverterParameter
属性传入,但是如果你使它成为MarkupExtension
,那么XAML解析器可以强制执行论证,以及要求论证存在。这使得转换器的内部非常简单。另外,你可以获得Intellisense。
答案 2 :(得分:0)
如果您不需要重复使用此组件或使其“通用”,则可以采用以下更简单且最集中的解决方案。
使用以下代码创建转换器:
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace WpfApplication1
{
public class CountToFontWeightConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null)
return DependencyProperty.UnsetValue;
var count = (int)value;
if (count > 10)
return FontWeights.Bold;
else
return FontWeights.Normal;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
然后以这种方式使用它:
<DataGrid.Resources>
<local:CountToFontWeightConverter x:Key="CountToFontWeightConverter"/>
<Style TargetType="{x:Type DataGridCell}" x:Key="DGCellStyle">
<Setter Property="FontWeight"
Value="{Binding OperativeCount,
Converter={StaticResource CountToFontWeightConverter}}"/>
</Style>
</DataGrid.Resources>
显然,如果OperativeCount
属性在您的应用生命周期内发生变化,则必须通过INotifyPropertyChanged
实施或Reactive
库提出变更通知。
你可以通过将10
的限制作为转换器的参数来概括一点这个解决方案,而不是在转换器本身内部对其进行硬编码,这样你就可以在几个不同的地方使用它