wpf 将 Button.IsEnabled 绑定到布尔属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31967085/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:19:57  来源:igfitidea点击:

Binding Button.IsEnabled to Boolean Property

c#wpfdata-binding

提问by aaron.anderson

I have a boolean property that looks at several checkboxes and returns true if any of them are checked. I would like to enable a button if any checkboxes are checked (property returns true).

我有一个布尔属性,它查看多个复选框,如果其中任何一个被选中,则返回 true。如果选中任何复选框(属性返回 true),我想启用一个按钮。

Currently I have the following:

目前我有以下几点:

  • The data context set

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }
    
  • The button binding set

    <Button Name="button" IsEnabled="{Binding ButtonEnabled}">Apply</Button>
    
  • The property

    public bool ButtonEnabled
    {
        get
        {
            if(checkboxes_enabled)
                return true;
            else
                return false;
        }
    }
    
  • 数据上下文集

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }
    
  • 按钮绑定集

    <Button Name="button" IsEnabled="{Binding ButtonEnabled}">Apply</Button>
    
  • 该物业

    public bool ButtonEnabled
    {
        get
        {
            if(checkboxes_enabled)
                return true;
            else
                return false;
        }
    }
    

I have verified that the property is updating as it is supposed to, so it's narrowed down to a binding issue. I have also tried data triggers within the button:

我已验证该属性正在按预期更新,因此将其范围缩小为具有约束力的问题。我还尝试了按钮内的数据触发器:

<Button Name="button" Content="Apply">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding ButtonEnabled}" Value="True">
                    <Setter Property="IsEnabled" Value="True"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding ButtonEnabled}" Value="False">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

回答by d.moncada

Two things:

两件事情:

You need INotifyPropertyChangedif you are making updates to a property that is bound.

你需要INotifyPropertyChanged,如果你正在更新绑定属性。

public class MyClass
{
    private bool _buttonEnabled;
    public bool ButtonEnabled
    {
        get
        {
            return _buttonEnabled;
        }
        set
        {
            _buttonEnabled = value;
            OnPropertyChanged();
        }
    }

    public SetButtonEnabled()
    {
        ButtonEnabled = checkboxes_enabled;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged<T>([CallerMemberName]string caller = null) 
    {
        var handler = PropertyChanged;
        if (handler != null) 
        {
            handler(this, new PropertyChangedEventArgs(caller));
        }
    }
}

You should also not have two triggers, and just use a default value.

您也不应该有两个触发器,而只使用默认值。

<Button Name="button" Content="Apply">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="IsEnabled" Value="True"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ButtonEnabled}" Value="False">
                    <Setter Property="IsEnabled" Value="False"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

回答by Daniel_Uns

I Would suggest binding the button to a command rather then an event, This way you can just set the command's "canexecute" property to false and disable the whole command that will intern disable the button for you.

我建议将按钮绑定到命令而不是事件,这样您就可以将命令的“canexecute”属性设置为 false 并禁用整个命令,该命令将为您禁用按钮。

I recommend the below tutorial to get a good understanding on WPF commands and how to use them, Once you understand how they work I find they are extremely useful.

我推荐下面的教程来更好地理解 WPF 命令以及如何使用它们,一旦你理解了它们的工作原理,我就会发现它们非常有用。

http://www.codeproject.com/Articles/274982/Commands-in-MVVM#hdiw1

http://www.codeproject.com/Articles/274982/Commands-in-MVVM#hdiw1

回答by tom jerry

you need to add the following code to implement INotifyPropertyChanged

您需要添加以下代码来实现 INotifyPropertyChanged

public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}

then call OnPropertyChanged from the property setter

然后从属性设置器调用 OnPropertyChanged