将布尔绑定到按钮可见性

Binding a bool to button visibility

我已经按照此 question 中的两个示例进行操作,但是我无法将视图模型中的布尔值绑定到视图中的按钮。我正在使用一个不同的按钮来切换另一个按钮是否应该可见,任何人都可以看到哪里出了问题吗?

目前我正在尝试将布尔值绑定到按钮的可见性,这不起作用,即使我已经测试过布尔值在 True/False 之间切换。我使用的另一种方法是使用数据触发器来更改可见性。我已将此方法注释掉,因为它不起作用。

查看

<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" />

查看模型

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;
    }

}

不要设置本地 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>

本地值优先于样式设置器,如文档中所述:https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-value-precedence

您还应该在视图模型中引发 ShowButtons 属性 的 PropertyChanged 事件:

public class CabinViewViewModel : BindableBase, ICabinViewViewModel
{
    private bool _showButtons;
    public bool ShowButtons
    {
        get { return _showButtons; }
        set { SetProperty(ref _showButtons, value); }
    }
    ...
}

显然 @mm8 的答案同样有效,但恕我直言,更简洁的方法是像这样使用 BooleanToVisibilityConverter:

<Button Visibility="{Binding ShowButtons, Converter={StaticResource BooleanToVisibilityConverter}}">

也为您节省了大量代码并且更易于阅读。