按钮可见性取决于两个属性(或不是AND)

时间:2015-10-16 08:11:40

标签: wpf button data-binding visibility multibinding

我在WPF中有一个按钮,如果鼠标位于按钮所在的边框上,我想让它可见。这很简单:

                    <Button Grid.Column="1" Click="DimFilter_Click" Style="{StaticResource ImageButton}" MouseDown="Button_MouseDown"
                            Visibility="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}, Path=IsMouseOver, Converter={StaticResource BoolToVis}}">
                        <Button.Content>
                            <Image Source="{Binding FilterActive, Converter={StaticResource FilterImageConverter}}"
                                   HorizontalAlignment="Center" VerticalAlignment="Center"/>
                        </Button.Content>
                    </Button>

我的问题是,如果属性&#34; FilterActive&#34;我还想让按钮可见。是真的。 所以如果&#34; FilterActive&#34;是假的我想在上面的代码中使用MouseOver Binding,但是如果&#34; FilterActive&#34;是的,Button应该始终可见,忽略第一个绑定。 这有可能吗?

2 个答案:

答案 0 :(得分:1)

好的,我知道了,愚蠢的我。

<Button.Visibility>
    <MultiBinding Converter="{StaticResource ButtonVisConverter}">
          <Binding Path="FilterActive"/>
          <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Border}}" Path="IsMouseOver"/>
     </MultiBinding>
</Button.Visibility>

转换器中的代码非常自我解释

答案 1 :(得分:0)

您可以使用触发器而不是绑定:

<Button Grid.Column="1" 
        Click="DimFilter_Click" 
        MouseDown="Button_MouseDown">                     
    <Button.Style>
        <Style BasedOn="{StaticResource ImageButton}" TargetType="Button">

            <Setter Property="Visibility" Value="Collapsed" />

            <Style.Triggers>                              
                <DataTrigger Binding="{Binding FilterActive}" Value="True">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>

                <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Border}}, Path=IsMouseOver, Converter={StaticResource BoolToVis}}" Value="True">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>                                  
            </Style.Triggers>

        </Style>
    </Button.Style>
    <Button.Content>
        <Image Source="{Binding FilterActive, Converter={StaticResource FilterImageConverter}}"
                    HorizontalAlignment="Center" VerticalAlignment="Center"/>
    </Button.Content>
</Button>