具有不同绑定的常见 WPF 样式 [英] Common WPF style with different bindings

查看:23
本文介绍了具有不同绑定的常见 WPF 样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中对多个椭圆使用以下样式.有没有更简单的方法可以使样式通用但更改绑定?我不想一遍又一遍地复制相同的代码.

I want to utilise the following style on multiple ellipse in my application. Is there an easier way of making the style common but change the binding? I would prefer not replicate the same code over and over again.

<Style TargetType="Ellipse" x:Key="TeamColors">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=ColorNo}" Value="1">
            <Setter Property="Fill" Value="Red"></Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=ColorNo}" Value="2">
            <Setter Property="Fill" Value="Yellow"></Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=ColorNo}" Value="3">
            <Setter Property="Fill" Value="Lime"></Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=ColorNo}" Value="4">
            <Setter Property="Fill" Value="Black"></Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=ColorNo}" Value="0">
            <Setter Property="Fill" Value="Gray"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

<Style TargetType="Ellipse" x:Key="TeamColors2">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Path=ColorNo2}" Value="1">
            <Setter Property="Fill" Value="Red"></Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=ColorNo2}" Value="2">
            <Setter Property="Fill" Value="Yellow"></Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=ColorNo2}" Value="3">
            <Setter Property="Fill" Value="Lime"></Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=ColorNo2}" Value="4">
            <Setter Property="Fill" Value="Black"></Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Path=ColorNo2}" Value="0">
            <Setter Property="Fill" Value="Gray"></Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>

推荐答案

有没有更简单的方法可以使样式通用但更改绑定?

Is there an easier way of making the style common but change the binding?

恐怕不行.您不能继承" DataTrigger 并仅更改 XAML 中的绑定路径.这不受支持.

I am afraid not. You can't "inherit" a DataTrigger and change only the binding path in XAML. This is not supported.

您可以考虑使用 C# 等编程语言以编程方式创建样式:

What you could consider to do is to create the styles programmatically using a programming language like C#:

private static Style CreateStyle(string bindingPath)
{
    const string xaml = "<Style xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" TargetType=\"Ellipse\">" +
        "<Style.Triggers>" +
            "<DataTrigger Binding=\"{{Binding Path={0}}}\" Value=\"1\">" +
             "   <Setter Property=\"Fill\" Value=\"Red\"></Setter>" +
            "</DataTrigger>" +
            "<DataTrigger Binding=\"{{Binding Path={0}}}\" Value=\"2\">" +
            "    <Setter Property=\"Fill\" Value=\"Yellow\"></Setter>" +
            "</DataTrigger>" +
            "<DataTrigger Binding=\"{{Binding Path={0}}}\" Value=\"3\">" +
            "    <Setter Property=\"Fill\" Value=\"Lime\"></Setter>" +
            "</DataTrigger>" +
            "<DataTrigger Binding=\"{{Binding Path={0}}}\" Value=\"4\">" +
            "    <Setter Property=\"Fill\" Value=\"Black\"></Setter>" +
            "</DataTrigger>" +
            "<DataTrigger Binding=\"{{Binding Path={0}}}\" Value=\"0\">" +
            "    <Setter Property=\"Fill\" Value=\"Gray\"></Setter>" +
            "</DataTrigger>" +
        "</Style.Triggers>" +
    "</Style>";

    return System.Windows.Markup.XamlReader.Parse(string.Format(xaml, bindingPath)) as Style;
}

示例用法:

Style style1 = CreateStyle("ColorNo");
Style style2 = CreateStyle("ColorNo2");
Application.Current.Resources.Add("TeamColors", style);
Application.Current.Resources.Add("TeamColors2", style2);

这篇关于具有不同绑定的常见 WPF 样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆