【问题标题】:Checkbox dependency复选框依赖
【发布时间】:2018-08-23 14:14:32
【问题描述】:

我有两个不同的对象列表。此列表在 UI 上表示,就像两个带有复选框的单独面板。我有一个带有一些命令的按钮。如何在 Button 中为两个对象、两个不同的列表绑定 IsEnabled 属性。我只想在选中一个列表中的复选框和另一个列表中的复选框时启用我的按钮。我正在使用 MVVM 模式并使用 Prism 框架。 谢谢。

【问题讨论】:

  • 您可以将 2 个 UI 元素绑定到同一个 ViewModel 属性。创建一个 bool 属性,并将 2 个不同的复选框连接到该属性。

标签: c# wpf mvvm binding


【解决方案1】:

您应该在您的视图模型中创建一个表示此状态的只读属性,例如:

public class YourViewModel : INotifyPropertyChanged
{
    private bool _c1, _c2;
    public bool Condition1
    {
        get { return _c1; }
        set
        {
            _c1 = value;
            //notify framework
            NotifyPropertyChanged(nameof(IsOtherThingEnabled))
        }
    }

    public bool Condition2
    {
        get { return _c2; }

        set
        {
            _c2 = value;
            //notify framework
            NotifyPropertyChanged(nameof(IsOtherThingEnabled))
        }
    }

    public bool IsOtherThingEnabled
    {
        get
        {
            return _c1 && _c2; 
        }
    }
    //etc.
}

NotifyPropertyChanged(nameof(IsOtherThingEnabled)) 的额外调用将​​触发框架更新此属性的视图绑定。

注意:取消 notifypropertychanged 的​​一些逻辑,例如 if (_c1 == value) { return; } 被省略。

补充说明:它可能不完全适合,但基本上是这个概念;我的 xaml,WPF 有点生疏了。

关于 XAML 绑定:

<!-- assuming your viewmodel is bound to the view -->
<button IsEnabled="{Binding IsOtherThingEnabled}"/>

【讨论】:

  • 非常感谢
  • @DraganMilovac93:欢迎您。请记住,感谢是通过投票和/或标记为答案来表达的;-)
猜你喜欢
  • 2014-08-17
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 1970-01-01
  • 2015-08-14
相关资源
最近更新 更多