我在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>
我的问题是,我也想使按钮可见,如果属性"FilterActive"是真。因此,如果"FilterActive"为假,我想在上面的代码中使用MouseOver绑定,但如果"FilterActive"为真,按钮应该一直可见,忽略第一个绑定。这有可能吗?
您可以使用触发器而不是绑定:
<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>
好吧,我懂了,我真笨。
<Button.Visibility>
<MultiBinding Converter="{StaticResource ButtonVisConverter}">
<Binding Path="FilterActive"/>
<Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type Border}}" Path="IsMouseOver"/>
</MultiBinding>
</Button.Visibility>
转换器中的代码几乎是不言自明的