在 WPF 中隐藏按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3052593/
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
Hiding a button in WPF
提问by sara
I want to hide a button in WPF used in a Windows form e.g., button.visible=false
我想在 Windows 窗体中使用的 WPF 中隐藏一个按钮,例如, button.visible=false
Is there an equivalent for this in WPF?
WPF 中是否有这样的等价物?
回答by Jakob Christensen
Try one of these:
尝试以下方法之一:
button.Visibility = Visibility.Hidden;
button.Visibility = Visibility.Collapsed;
Hidden
hides the button but the button will still take up space in the UI. Collapsed
will collapse the button such that it has zero width and height.
Hidden
隐藏按钮,但按钮仍会占用 UI 中的空间。 Collapsed
将折叠按钮,使其宽度和高度为零。
回答by Gianluca Colucci
You should set
你应该设置
button.visibility = System.Windows.Visibility.Hidden;
button.visibility = System.Windows.Visibility.Hidden;
or
或者
button.visibility = System.Windows.Visibility.Collapsed;
button.visibility = System.Windows.Visibility.Collapsed;
or by using the WPF XAML property set the same...
或通过使用 WPF XAML 属性设置相同...
回答by Franti?ek ?ia?ik
Visibility = Hidden
Visibility = Hidden
回答by Martin
If you use the MVVM design pattern, you can make use of the "CanExecute" state of the command that is bound to the button in order to achieve this.
如果您使用 MVVM 设计模式,您可以利用绑定到按钮的命令的“CanExecute”状态来实现这一点。
The default behaviour for the button would be to be displayed disabled, but you can change this so it will not be visible using the example below.
该按钮的默认行为是禁用显示,但您可以使用以下示例更改此设置,使其不可见。
ViewModel
视图模型
Property for command property:
命令属性的属性:
public DelegateCommand<TypeOfBoundItem> TheCommand
{
get; private set;
}
Instanciate the command (in constructor of view model):
实例化命令(在视图模型的构造函数中):
TheCommand = new DelegateCommand<TypeOfBoundItem>( Execute, CanExecute );
The methods to execute the command and to decide whether it can be executed:
执行命令和决定是否可以执行的方法:
private bool CanExecute( TypeOfBoundItem item )
{
return true or return false;
}
private bool Execute()
{
// some logic
}
View
看法
In the resources of the UserControl or Window you define some visibilty converter:
在 UserControl 或 Window 的资源中,您定义了一些可见性转换器:
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
For the button, you define a binding setting the "Visibility" attribute of the button to the value of the buttons "IsEnabled" state, applying the converter.
对于按钮,您定义一个绑定,将按钮的“可见性”属性设置为按钮“IsEnabled”状态的值,应用转换器。
So if the "IsEnabled" state is true - because the bound "TheCommand" returns true in its "CanExecute" method - the button will be visible.
因此,如果“IsEnabled”状态为真——因为绑定的“TheCommand”在其“CanExecute”方法中返回真——按钮将可见。
If the "CanExecute" method of the bound command returns false, the button will not be visible.
如果绑定命令的“CanExecute”方法返回false,按钮将不可见。
<Button Command="{Binding TheCommand}"
CommandParameter="{Binding}"
Visibility="{Binding RelativeSource={RelativeSource Self},
Path=IsEnabled,
UpdateSourceTrigger=PropertyChanged,
Converter={StaticResource BooleanToVisibilityConverter}}">
</Button>