[英]Binding a bool to button visibility
I have followed both examples in this question however I am unable to bind a boolean in my view model to a button in my view. 我在这个问题中都遵循了两个示例,但是我无法将视图模型中的布尔值绑定到视图中的按钮。 I am using a different button that toggles whether or not the other button should be visible or not, can anyone see what is wrong?
我正在使用另一个按钮来切换另一个按钮是否应该可见,任何人都可以看到问题所在吗?
Currently I am trying to bind the boolean to the visibility of the button, this is not working even though I have tested that the boolean is toggling between True/False. 目前,我正在尝试将布尔值绑定到按钮的可见性,即使我已经测试过布尔值在True / False之间切换,这也无法正常工作。 The other method I used was to use a data trigger to change the visibility.
我使用的另一种方法是使用数据触发器来更改可见性。 I have commented out this method as it was not working.
我已注释掉此方法,因为它不起作用。
View 视图
<Button Canvas.Left="300" Canvas.Top="235" Click="ShowEquipmentItems" Cursor="Hand" Visibility="{Binding ShowButtons, Converter={StaticResource BooleanToVisibilityConverter}}">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Content">
<Setter.Value>
<StackPanel>
<Image Source="../Images/Green spot icon.png" Height="35" Width="35" />
</StackPanel>
</Setter.Value>
</Setter>
<Setter Property="Visibility" Value="Collapsed"/>
<!--<Style.Triggers>
<DataTrigger Binding="{Binding ShowButtons}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>-->
</Style>
</Button.Style>
</Button>
<Button Canvas.Right="10" Canvas.Top="388" Command="{Binding ToggleButtonCommand}" Cursor="Hand" Height="50" Width="50" />
View Model 查看模型
public class CabinViewViewModel : BindableBase, ICabinViewViewModel
{
public bool ShowButtons { get; set; }
public DelegateCommand ToggleButtonCommand { get; private set; }
public CabinViewViewModel()
{
ToggleButtonCommand = new DelegateCommand(ToggleButtons, CanToggleButtons);
ShowButtons = true;
}
public void ToggleButtons()
{
ShowButtons = !ShowButtons;
System.Console.WriteLine("Toggle Buttons" + ShowButtons.ToString());
}
public bool CanToggleButtons()
{
return true;
}
}
Obviously @mm8's answer works just as fine but imho a cleaner way is to use the BooleanToVisibilityConverter like this: 显然,@ mm8的答案同样有效,但恕我直言,更干净的方法是使用BooleanToVisibilityConverter,如下所示:
<Button Visibility="{Binding ShowButtons, Converter={StaticResource BooleanToVisibilityConverter}}">
Saves you a lot of code, too and is easier to read. 也节省了很多代码,并且更易于阅读。
Don't set the local Visibility
property: 不要设置本地
Visibility
属性:
<Button Canvas.Left="300" Canvas.Top="235" Click="ShowEquipmentItems" Cursor="Hand">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Content">
<Setter.Value>
<StackPanel>
<Image Source="../Images/Green spot icon.png" Height="35" Width="35" />
</StackPanel>
</Setter.Value>
</Setter>
<Setter Property="Visibility" Value="Collapsed"/>
<Style.Triggers>
<DataTrigger Binding="{Binding ShowButtons}" Value="True">
<Setter Property="Visibility" Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Local values take precedence over style setters as explained in the docs: https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence 如文档中所述,本地值优先于样式设置器: https : //docs.microsoft.com/zh-cn/dotnet/framework/wpf/advanced/dependency-property-value-precedence
You should also raise the PropertyChanged
event for the ShowButtons
property in your view model: 您还应该在视图模型中
ShowButtons
属性的PropertyChanged
事件:
public class CabinViewViewModel : BindableBase, ICabinViewViewModel
{
private bool _showButtons;
public bool ShowButtons
{
get { return _showButtons; }
set { SetProperty(ref _showButtons, value); }
}
...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.