数据触发器:当大于一个数字时

3

我有一个名为 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"无法正常工作,但本质上这就是我想做的。

这个回答解决了你的问题吗?WPF触发器,如果值相等或更大,则会起作用 - StayOnTarget
2个回答

4

使用 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来处理它。

对我来说很难找到这个软件,所以这里是目前最相关的链接: https://www.microsoft.com/en-us/download/details.aspx?id=5915 和 https://www.microsoft.com/en-us/download/details.aspx?id=10801 请注意,在第一个链接中的“概述”中可能会有一些新的(但不清楚的)信息! - F.I.V
您正在引用 Expression Studio 的旧版本。Expression Studio 现已停用,Expression Blend 已包含在 Visual Studio 中。只需获取 Visual Studio 的社区版即可。 - Raamakrishnan A.

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的限制作为转换器的参数传递来稍微概括一下此解决方案,而不是在转换器本身中进行硬编码,因此您可以在多个地方使用具有不同限制的它。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接