具有不同绑定的常见WPF样式

时间:2018-02-06 18:43:38

标签: c# wpf xaml

我想在我的应用程序中在多个椭圆上使用以下样式。有没有更简单的方法使风格变得常见但更改绑定?我不希望一遍又一遍地复制相同的代码。

<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>

1 个答案:

答案 0 :(得分:0)

  

是否有更简单的方法使风格变得常见但更改了绑定?

我不敢。您不能“继承”DataTrigger并仅更改XAML中的绑定路径。这不受支持。

您可以考虑使用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);