在回顾我多年前写的一些代码时,我在WPF样式部分遇到了这个触发器。可以看出,我使用与Checkin,Checkout和NotSeen完全相同的引用调用MultiBinding Converter 7次,以产生7种不同的颜色。这让我感到非常多余和占用空间。必须有更好的方法。
有没有办法用更短的风格来完成这个?另外,他们是一种在MVVM框架中完全避免MultiBinding Converter的方法吗?是否可以使用附加行为代替?
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>
答案 0 :(得分:1)
使用Custom DataTrigger的第一种方法 创建自定义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>