我想用转换器创建一个触发器,如果其中一个绑定属性为true,则返回true。
像这样public class TestConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values != null && values.All(x => x is bool))
return values.OfType<bool>().Any(x => x);
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
并在按钮中实现
<Button Width="100" Height="100" Content="CONTENT">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Value="True" >
<Condition.Binding>
<MultiBinding>
<MultiBinding.Converter>
<WpfApplication2:TestConverter />
</MultiBinding.Converter>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Button.IsMouseOver"></Binding>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Button.IsKeyboardFocusWithin"></Binding>
</MultiBinding>
</Condition.Binding>
</Condition>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Foreground" Value="White" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>
但我在转换器中获得的唯一值是DependencyProperty.Unsetvalue。有谁知道谁修复这个Multibinding?
我也试过
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Button}}" Path="IsMouseOver"></Binding>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Button}}" Path="IsKeyboardFocusWithin"></Binding>
但这也行不通
答案 0 :(得分:5)
您需要将Binding
更改为{RelativeSource Self}
并从Button
中删除Binding.Path
。另一件事是你真的不需要MultiTrigger
。在您的情况下DataTrigger
MultiBinding
就足够了:
<Button Width="100" Height="100" Content="CONTENT">
<Button.Template>
<ControlTemplate TargetType="Button">
<Grid Background="{TemplateBinding Background}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
<ControlTemplate.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding>
<MultiBinding.Converter>
<WpfApplication2:TestConverter />
</MultiBinding.Converter>
<Binding RelativeSource="{RelativeSource Self}" Path="IsMouseOver"></Binding>
<Binding RelativeSource="{RelativeSource Self}" Path="IsKeyboardFocusWithin"></Binding>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Foreground" Value="White" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Button.Template>
</Button>