wpf - 数据触发重复使用

发布于 2024-10-06 13:26:46 字数 252 浏览 1 评论 0原文

我有当前的 DataTrigger:

<DataTrigger Binding="{Binding HeaderType}" Value="1">
   <Setter Property="BorderThickness" Value="5"/></DataTrigger>

我想对值 2-100 执行相同的操作

我是否必须复制数据触发器 99 次或者也许有更好的方法?

I have the current DataTrigger:

<DataTrigger Binding="{Binding HeaderType}" Value="1">
   <Setter Property="BorderThickness" Value="5"/></DataTrigger>

I want to do the same with values 2-100

Do I have to copy the Data Trigger 99 times or maybe there's a better way ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

话少情深 2024-10-13 13:26:46

向视图模型添加属性:

public bool HasImportantHeader // or something...
{
   get { return HeaderType >=1 && HeaderType <= 100; }
}

在数据触发器中使用该属性:

<DataTrigger Binding="{Binding HasImportantHeader}" Value="True">   
   <Setter Property="BorderThickness" Value="5"/>
</DataTrigger>

通常,我喜欢使 XAML 尽可能简单,将所有逻辑放在视图模型中,并避免使用转换器,除非绝对必要。

假设您添加另一个视图,您希望在其中使用粗体文本来指示标头类型介于 1 和 100 之间。只需重新使用 HasImportantHeader 属性,例如:

<DataTrigger Binding="{Binding HasImportantHeader}" Value="True">   
   <Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>

稍后,您可能会决定所有最多 200 的标题类型应具有粗边框和粗体文本。更改 HasImportantHeader 属性的实现非常简单。

Add a property to your view model:

public bool HasImportantHeader // or something...
{
   get { return HeaderType >=1 && HeaderType <= 100; }
}

Use that property in the data trigger:

<DataTrigger Binding="{Binding HasImportantHeader}" Value="True">   
   <Setter Property="BorderThickness" Value="5"/>
</DataTrigger>

Generally, I like to keep my XAML as simple as possible, put all the logic in the view model, and avoid using Converters unless they are absolutely necessary.

Let's say you add another view, where you want to use bold text to indicate the header type is between 1 and 100. Just re-use the HasImportantHeader property, something like:

<DataTrigger Binding="{Binding HasImportantHeader}" Value="True">   
   <Setter Property="FontWeight" Value="Bold"/>
</DataTrigger>

Later, you may decide that all header types up to 200 should have thick border and bold text. It'll be a simple matter of changing the implementation of the HasImportantHeader property.

陈独秀 2024-10-13 13:26:46

我在类似的情况下使用过它

<DataTrigger Binding="{Binding HeaderType,
                               Converter={StaticResource RangeConverter},
                               ConverterParameter=1-100}"
             Value="True">
    <Setter Property="BorderThickness" Value="5"/>
</DataTrigger>

并且在转换器中我们根据范围返回 true 或 false

public class RangeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string[] ranges = parameter.ToString().Split(new char[]{'-'});
        int headerType = (int)value;
        if (headerType >= System.Convert.ToInt32(ranges[0]) &&
            headerType <= System.Convert.ToInt32(ranges[1]))
        {
            return true;
        }
        return false;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}

I've used this in similar situations

<DataTrigger Binding="{Binding HeaderType,
                               Converter={StaticResource RangeConverter},
                               ConverterParameter=1-100}"
             Value="True">
    <Setter Property="BorderThickness" Value="5"/>
</DataTrigger>

And in the converter we return true or false depending on the ranges

public class RangeConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        string[] ranges = parameter.ToString().Split(new char[]{'-'});
        int headerType = (int)value;
        if (headerType >= System.Convert.ToInt32(ranges[0]) &&
            headerType <= System.Convert.ToInt32(ranges[1]))
        {
            return true;
        }
        return false;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return null;
    }
}
凉栀 2024-10-13 13:26:46

为此,您需要使用转换器。您可以在 DataTrigger 上添加转换器。
转换器将允许您传入值,并返回 true 或 false。

<DataTrigger
  Binding="{Binding HeaderType, Converter={StaticResource RengeConvertor}}"
  Value="true"
  >
  <Setter Property="BorderThickness" Value="5" />
</DataTrigger>

你的转换器应该看起来像

public class RengeConvertor : IValueConverter

{

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

{

    int data = (int)value;

    if (data >= 2 && data <= 100)

        return true;

    else

        return false;

}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

{

    throw new NotImplementedException();

}

}

你可能还会发现这个有趣的 http:// /zamjad.wordpress.com/2010/07/29/range-converter/

You need to use a converter for that purpose.You can add a converter on your DataTrigger.
The Converter will allow you to pass in the value, and return true or false.

<DataTrigger
  Binding="{Binding HeaderType, Converter={StaticResource RengeConvertor}}"
  Value="true"
  >
  <Setter Property="BorderThickness" Value="5" />
</DataTrigger>

and your converter should look something like

public class RengeConvertor : IValueConverter

{

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)

{

    int data = (int)value;

    if (data >= 2 && data <= 100)

        return true;

    else

        return false;

}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

{

    throw new NotImplementedException();

}

}

You may also find this interesting http://zamjad.wordpress.com/2010/07/29/range-converter/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文