如何获得DataTemplate。是否要检查大于或小于?

时间:2022-04-26 06:56:44

The following DataTemplate.DataTrigger makes the age display red if it is equal to 30.

以下DataTemplate。DataTrigger使年龄显示红色,如果它等于30。

How do I make the age display red if it is greater than 30?

如果年龄大于30岁,我如何将年龄显示为红色?

<DataTemplate DataType="{x:Type local:Customer}">
    <Grid x:Name="MainGrid" Style="{StaticResource customerGridMainStyle}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"/>
            <ColumnDefinition Width="150"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <TextBlock Grid.Column="0" Grid.Row="0" Text="First Name" Margin="5"/>
        <TextBlock Grid.Column="1" Grid.Row="0" Text="{Binding FirstName}" Margin="5"/>
        <TextBlock Grid.Column="0" Grid.Row="1" Text="Last Name" Margin="5"/>
        <TextBlock Grid.Column="1" Grid.Row="1" Text="{Binding LastName}" Margin="5"/>
        <TextBlock Grid.Column="0" Grid.Row="2" Text="Age" Margin="5"/>
        <TextBlock x:Name="Age" Grid.Column="1" Grid.Row="2" Text="{Binding Age}" Margin="5"/>
    </Grid>

    <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding Path=Age}">
            <DataTrigger.Value>30</DataTrigger.Value>
            <Setter TargetName="Age" Property="Foreground" Value="Red"/> 
        </DataTrigger>
    </DataTemplate.Triggers>

</DataTemplate>

3 个解决方案

#1


63  

You could create an IValueConverter, which converts an integer to a boolean based on the CutOff. Then use DataTrigger.Value of True (or False, depending on what you are returning).

您可以创建一个IValueConverter,它基于截止值将整数转换为布尔值。然后使用DataTrigger。True值(或False,取决于您返回的内容)。

WPF DataTriggers are strictly equality comparers if I remember correctly.

如果我没记错的话,WPF数据处理程序是完全平等的比较程序。

So something similar to:

所以类似于:

public class CutoffConverter : IValueConverter {
    object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        return ((int)value) > Cutoff;
    }

    object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }

    public int Cutoff { get; set; }
}

Then use the following XAML.

然后使用以下XAML。

<Window.Resources>
    <myNamespace:CutoffConverter x:Key="AgeConverter" Cutoff="30" />
</Window.Resources>

<DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=Age,
                                   Converter={StaticResource AgeConverter}}">
        <DataTrigger.Value>true</DataTrigger.Value>
        <Setter TargetName="Age" Property="Foreground" Value="Red"/> 
    </DataTrigger>
</DataTemplate.Triggers>

#2


10  

I'd recommend using an IValueConverter to bind to the Foreground element of the Age TextBlock and isolating the coloring logic there.

我建议使用IValueConverter绑定到Age TextBlock的前台元素,并在那里隔离着色逻辑。

<TextBlock x:Name="Age" Grid.Column="1" Grid.Row="2" Text="{Binding Age}" Foreground="{Binding Path=Age, Converter={StaticResource AgeToColorConverter}}" Margin="5"/>

Then in the Code:

然后在代码:

[ValueConversion(typeof(int), typeof(Brush))]
public class AgeToColorConverter : IValueConverter
{
   public object Convert(object value, Type target)
   {
      int age;
      Int32.TryParse(value.ToString(), age);
      return (age >= 30 ? Brushes.Red : Brushes.Black);
   }
}

#3


8  

I believe there is a simpler way of acheiving the goal by using the powers of MVVM and INotifyPropertyChanged.

我相信有一种更简单的方法通过使用MVVM和INotifyPropertyChanged的功能来实现目标。


With the Age property create another property which will be a boolean called IsAgeValid. The IsAgeValid will simply be an on demand check which does not technically need an the OnNotify call. How?

使用Age属性创建另一个属性,该属性将是一个名为IsAgeValid的布尔值。IsAgeValid只是一个按需检查,技术上不需要OnNotify调用。如何?

To get changes pushed to the Xaml, place the OnNotifyPropertyChanged for IsAgeValid within the Age setter instead.

为了将更改推送到Xaml,将为IsAgeValid的OnNotifyPropertyChanged放在Age setter中。

Any binding to IsAgeValid will have a notify message sent on any Age change which is really what is being looked at.

任何对IsAgeValid的绑定都会有一个关于年龄变化的通知信息,这正是我们正在关注的。


Once setup, of course bind the style trigger for false and true accordingly to the IsAgeValid result.

设置之后,当然要将样式触发器绑定到IsAgeValid结果。

public bool IsAgeValid{ get { return Age > 30; } }

public int Age
{ 
  get { return _Age; }

  set
  {
   _Age=value;
   OnPropertyChanged("Age");   
   OnPropertyChanged("IsAgeValid"); // When age changes, so does the
                                    // question *is age valid* changes. So 
                                    // update the controls dependent on it.
   } 
 }

#1


63  

You could create an IValueConverter, which converts an integer to a boolean based on the CutOff. Then use DataTrigger.Value of True (or False, depending on what you are returning).

您可以创建一个IValueConverter,它基于截止值将整数转换为布尔值。然后使用DataTrigger。True值(或False,取决于您返回的内容)。

WPF DataTriggers are strictly equality comparers if I remember correctly.

如果我没记错的话,WPF数据处理程序是完全平等的比较程序。

So something similar to:

所以类似于:

public class CutoffConverter : IValueConverter {
    object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
        return ((int)value) > Cutoff;
    }

    object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
        throw new NotImplementedException();
    }

    public int Cutoff { get; set; }
}

Then use the following XAML.

然后使用以下XAML。

<Window.Resources>
    <myNamespace:CutoffConverter x:Key="AgeConverter" Cutoff="30" />
</Window.Resources>

<DataTemplate.Triggers>
    <DataTrigger Binding="{Binding Path=Age,
                                   Converter={StaticResource AgeConverter}}">
        <DataTrigger.Value>true</DataTrigger.Value>
        <Setter TargetName="Age" Property="Foreground" Value="Red"/> 
    </DataTrigger>
</DataTemplate.Triggers>

#2


10  

I'd recommend using an IValueConverter to bind to the Foreground element of the Age TextBlock and isolating the coloring logic there.

我建议使用IValueConverter绑定到Age TextBlock的前台元素,并在那里隔离着色逻辑。

<TextBlock x:Name="Age" Grid.Column="1" Grid.Row="2" Text="{Binding Age}" Foreground="{Binding Path=Age, Converter={StaticResource AgeToColorConverter}}" Margin="5"/>

Then in the Code:

然后在代码:

[ValueConversion(typeof(int), typeof(Brush))]
public class AgeToColorConverter : IValueConverter
{
   public object Convert(object value, Type target)
   {
      int age;
      Int32.TryParse(value.ToString(), age);
      return (age >= 30 ? Brushes.Red : Brushes.Black);
   }
}

#3


8  

I believe there is a simpler way of acheiving the goal by using the powers of MVVM and INotifyPropertyChanged.

我相信有一种更简单的方法通过使用MVVM和INotifyPropertyChanged的功能来实现目标。


With the Age property create another property which will be a boolean called IsAgeValid. The IsAgeValid will simply be an on demand check which does not technically need an the OnNotify call. How?

使用Age属性创建另一个属性,该属性将是一个名为IsAgeValid的布尔值。IsAgeValid只是一个按需检查,技术上不需要OnNotify调用。如何?

To get changes pushed to the Xaml, place the OnNotifyPropertyChanged for IsAgeValid within the Age setter instead.

为了将更改推送到Xaml,将为IsAgeValid的OnNotifyPropertyChanged放在Age setter中。

Any binding to IsAgeValid will have a notify message sent on any Age change which is really what is being looked at.

任何对IsAgeValid的绑定都会有一个关于年龄变化的通知信息,这正是我们正在关注的。


Once setup, of course bind the style trigger for false and true accordingly to the IsAgeValid result.

设置之后,当然要将样式触发器绑定到IsAgeValid结果。

public bool IsAgeValid{ get { return Age > 30; } }

public int Age
{ 
  get { return _Age; }

  set
  {
   _Age=value;
   OnPropertyChanged("Age");   
   OnPropertyChanged("IsAgeValid"); // When age changes, so does the
                                    // question *is age valid* changes. So 
                                    // update the controls dependent on it.
   } 
 }