简化的风格.在WPF中具有多绑定的触发器



在回顾我多年前编写的一些代码时,我在WPF样式部分遇到了这个触发器。可以看到,我调用了7次MultiBinding Converter,并引用了Checkin、Checkout和NotSeen,从而产生了7种不同的颜色。这让我觉得非常冗余和占用空间。一定有更好的办法。

有没有办法用短一点的发型来达到这个效果?此外,它们是一种在MVVM框架中完全避免多绑定转换器的方法吗?是否可以使用附加行为来代替此行为?

TIA

    <Style.Triggers>
            <!-- if checkout is null, then check for waiting time from the checkin value. Setter action occurs for DataTrigger Value -->
            <DataTrigger Value="1" >
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource WaitStatus}">
                        <Binding Path="CheckIn" />
                        <Binding Path="CheckOut" />
                        <Binding Path="NotSeen" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="{StaticResource VioletBrush}" />
            </DataTrigger>
            <DataTrigger Value="2" >
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource WaitStatus}">
                        <Binding Path="CheckIn" />
                        <Binding Path="CheckOut" />
                        <Binding Path="NotSeen" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="{StaticResource BlueBrush}" />
            </DataTrigger>
            <DataTrigger Value="3" >
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource WaitStatus}">
                        <Binding Path="CheckIn" />
                        <Binding Path="CheckOut" />
                        <Binding Path="NotSeen" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="{StaticResource TurquoiseBrush}" />
            </DataTrigger>
            <DataTrigger Value="4" >
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource WaitStatus}">
                        <Binding Path="CheckIn" />
                        <Binding Path="CheckOut" />
                        <Binding Path="NotSeen" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="{StaticResource GreenBrush}" />
            </DataTrigger>
            <DataTrigger Value="5" >
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource WaitStatus}">
                        <Binding Path="CheckIn" />
                        <Binding Path="CheckOut" />
                        <Binding Path="NotSeen" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="{StaticResource YellowBrush}" />
            </DataTrigger>
            <DataTrigger Value="6" >
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource WaitStatus}">
                        <Binding Path="CheckIn" />
                        <Binding Path="CheckOut" />
                        <Binding Path="NotSeen" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="{StaticResource OrangeBrush}" />
            </DataTrigger>
            <DataTrigger Value="7" >
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource WaitStatus}">
                        <Binding Path="CheckIn" />
                        <Binding Path="CheckOut" />
                        <Binding Path="NotSeen" />
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="{StaticResource RedBrush}" />
            </DataTrigger>
        </Style.Triggers>

使用自定义数据触发器的第一种方法
创建自定义DataTrigger类,通过编程定义其绑定,但在代码中设置Setter。

class DataTriggerBinding : DataTrigger
{
    public DataTriggerBinding()
    {
        SetBinding();
    }
    private void SetBinding()
    {
        var multiBinding = new MultiBinding();
        multiBinding.Bindings.Add(new Binding("CheckIn"));
        multiBinding.Bindings.Add(new Binding("CheckOut"));
        multiBinding.Bindings.Add(new Binding("NotSeen"));
        multiBinding.Converter = new WaitStatus();
        this.Binding = multiBinding;
    }
}

XAML:

  <Label Content="{Binding Age}">
    <Label.Style>
        <Style TargetType="Label">
            <Style.Triggers>
                <local:DataTriggerBinding Value="1">
                    <Setter Property="Foreground" Value="Red"/>
                </local:DataTriggerBinding>
            </Style.Triggers>
        </Style>
    </Label.Style>
</Label>

后一种方法使用附加行为

class DataTriggerBinding : DependencyObject
{
    public static readonly DependencyProperty IsCustomBindingProperty = DependencyProperty.RegisterAttached(
        "CustomBinding", typeof(bool), typeof(DataTriggerBinding), new PropertyMetadata((x, y) =>
        {
            SetBinding((DataTrigger)x);
        }));
    public static void SetIsCustomBinding(DependencyObject element, bool value)
    {
        element.SetValue(IsCustomBindingProperty, value);
    }
    public static bool GetIsCustomBinding(DependencyObject element)
    {
        return (bool)element.GetValue(IsCustomBindingProperty);
    }
    private static void SetBinding(DataTrigger dataTrigger)
    {
        var multiBinding = new MultiBinding();
        multiBinding.Bindings.Add(new Binding("CheckIn"));
        multiBinding.Bindings.Add(new Binding("CheckOut"));
        multiBinding.Bindings.Add(new Binding("NotSeen"));
        multiBinding.Converter = new WaitStatus();
        dataTrigger.Binding = multiBinding;
    }
}

XAML:

    <DataTrigger local:DataTriggerBinding.IsCustomBinding="True" Value="1">
        <Setter Property="Foreground" Value="Red"/>
    </DataTrigger>

相关内容

  • 没有找到相关文章

最新更新