简体   繁体   English

使用linq表达式将IsEnabled绑定到linq属性

[英]Bind IsEnabled to linq property with linq expression

i would like to bind in XAML button's attribute "IsEnabled" to the condition, like "enable button only if all of items in my Observable collection have IsValid property = true". 我想将XAML按钮的属性“ IsEnabled”绑定到条件,例如“仅当我的Observable集合中的所有项目都具有IsValid属性= true时才启用按钮”。 So the Linq expression would look like: 因此,Linq表达式将如下所示:

MyObsCollectionProp.Any(record=>!record.IsValid)

or 要么

 MyObsCollectionProp.All(record=>record.IsValid)

Can sombody tell my the legal and valid (for MVVM pattern) way to do this? Sombody可以告诉我执行此操作的合法有效方法(针对MVVM模式)吗?

Declare a boolean property named IsButtonEnable as: 将名为IsButtonEnable的布尔属性声明为:

private bool isButtonEnable;
public bool IsButtonEnable
{
    get
    {
        return isButtonEnable;
    }
    set
    {
        isButtonEnable = value;
        OnPropertyChanged("IsButtonEnable");
    }
}

Bind a button with this property as: 将具有此属性的按钮绑定为:

<Button Content="Save Data" IsEnable="{Binding IsButtonEnable, UpdateSourceTrigger=PropertyChanged}"></Button>

Now in your ViewModel bind ObservableCollectionChanged event as: 现在,在您的ViewModel中,将ObservableCollectionChanged事件绑定为:

public MyViewModel()
{
    MyObsCollectionProp = new ObservableCollection<YourModel>();
    MyObsCollectionProp += MyObsCollectionProp_Changed;
}
void MyObsCollectionProp_Changed(object sender, NotifyCollectionChangedEventArgs e)
{
    // Handle here
    IsButtonEnable = MyObsCollectionProp.All(record=>record.IsValid)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM