wpf 绑定另一个元素的可见性 - 并反转
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4099960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Binding off Visibility of another element - and inverting
提问by Adam Rackis
I'd like to make Control A visibile if Control B is hidden, and vice versa. Right now I have this converter:
如果控件 B 被隐藏,我想让控件 A 可见,反之亦然。现在我有这个转换器:
public class InvertVisibilityConverter : IValueConverter {
public Object Convert(Object value, Type targetType, Object parameter, CultureInfo culture) {
if (targetType == typeof(Visibility)) {
Visibility vis = (Visibility)value;
return vis == Visibility.Collapsed ? Visibility.Visible : Visibility.Collapsed;
}
throw new InvalidOperationException("Converter can only convert to value of type Visibility.");
}
public Object ConvertBack(Object value, Type targetType, Object parameter, CultureInfo culture) {
throw new Exception("Invalid call - one way only");
}
}
And this XAML:
这个 XAML:
<Button Visibility="{Binding ElementName=btn1, Path=Visibility, Converter={StaticResource InvertVisibilityConverter}}">Btn2</Button>
Which works. I'm just wondering if there's a more direct way in WPF / Silverlight to accomplish this? I don't mind having a converter, I just want to make sure there's no better way I'm not aware of.
哪个有效。我只是想知道 WPF/Silverlight 中是否有更直接的方法来实现这一点?我不介意有一个转换器,我只是想确保没有我不知道的更好的方法。
采纳答案by Phil Sandler
Bottom line, I see no problem with what you are doing.
最重要的是,我认为你在做什么没有问题。
That said, if you have a property in the ViewModel to which Control A binds to control its visibility, I would bind Control B to the same property and invert the visibily via a separate converter. I'm not sure if I can articulate why, but that seems more natural to me (at least in lieu of any additional context around what you are doing).
也就是说,如果您在 ViewModel 中有一个属性,控件 A 绑定到该属性以控制其可见性,我会将控件 B 绑定到相同的属性并通过单独的转换器反转可见性。我不确定我是否能阐明原因,但这对我来说似乎更自然(至少代替任何关于你正在做的事情的额外背景)。
回答by Fredrik Hedblad
In this case you could do it with triggers. Example with two Buttons
在这种情况下,您可以使用触发器来完成。带有两个按钮的示例
<Button Name="button1">
<Button.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=button2, Path=Visibility}" Value="Visible">
<Setter Property="Button.Visibility" Value="Hidden"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
<Button Name="button2"/>
回答by Brad Cunningham
What you doing is fine but I tend to prefer more general converters and to keep symmetry with the built in converters.
你所做的很好,但我倾向于更喜欢更通用的转换器并与内置转换器保持对称。
So I would do a InverseBooleanToVisibiltyConverter that accepts booleans and returns visibility types. This matches with the built in BoolenToVisibiltyConverter
所以我会做一个 InverseBooleanToVisibiltyConverter 接受布尔值并返回可见性类型。这与内置的 BoolenToVisibiltyConverter 匹配
Then I would bind to the IsVisible property of the button not the Visibility property.
然后我将绑定到按钮的 IsVisible 属性而不是 Visibility 属性。
But that is a matter of preference really.
但这确实是一个偏好问题。