本文介绍了multidatatrigger在ControlTemplate.Triggers multibinding的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打一个触发和如果绑定属性之一是真的,返回true的转换器。

I want to make a trigger with a converter which returns true if one of the binded properties is 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?

But the only values I get in the converter are DependencyProperty.Unsetvalue. Do anyone knows who to fix this Multibinding?

我也试过

<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Button}}" Path="IsMouseOver"></Binding>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Button}}" Path="IsKeyboardFocusWithin"></Binding>

但也不能正常工作

but that also doesn't work

推荐答案

您需要更改绑定 {自我的RelativeSource} 并删除按钮 Binding.Path 。另一件事是,你并不真的需要 MultiTrigger DataTrigger MultiBinding 将足以在你的情况:

You'll need to change Binding to {RelativeSource Self} and remove Button from the Binding.Path. Another thing is that you don't really need MultiTrigger. DataTrigger with MultiBinding will be enough in your case:

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

这篇关于multidatatrigger在ControlTemplate.Triggers multibinding的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 22:17