c# - 使用 linq 表达式将 IsEnabled 绑定(bind)到 linq 属性

标签 c# linq xaml mvvm binding

我想将 XAML 按钮的属性“IsEnabled”绑定(bind)到条件,例如“仅当我的 Observable 集合中的所有项目都具有 IsValid 属性 = true 时才启用按钮”。所以 Linq 表达式看起来像:

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

或者
 MyObsCollectionProp.All(record=>record.IsValid)

有人可以告诉我这样做的合法有效(对于 MVVM 模式)方式吗?

最佳答案

将名为 IsButtonEnable 的 bool 属性声明为:

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

将具有此属性的按钮绑定(bind)为:
<Button Content="Save Data" IsEnable="{Binding IsButtonEnable, UpdateSourceTrigger=PropertyChanged}"></Button>

现在在您的 ViewModel 中将 ObservableCollectionChanged 事件绑定(bind)为:
public MyViewModel()
{
    MyObsCollectionProp = new ObservableCollection<YourModel>();
    MyObsCollectionProp += MyObsCollectionProp_Changed;
}
void MyObsCollectionProp_Changed(object sender, NotifyCollectionChangedEventArgs e)
{
    // Handle here
    IsButtonEnable = MyObsCollectionProp.All(record=>record.IsValid)
}

关于c# - 使用 linq 表达式将 IsEnabled 绑定(bind)到 linq 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45422921/

相关文章:

asp.net-mvc - LINQ 将日期时间转换为字符串

asp.net - 用于 ASP.NET MVC 的基于 XAML 的 View 引擎

c# - 在这种情况下使用多态性优于枚举有什么好处吗?

c# - 如何比较两个字符串数组并将匹配的值存储在另一个字符串数组中?

java - 在 JAX-RS 中使用位置 header 创建响应

c# - 从后面的代码添加 div 元素的宽度

asp.net-mvc - System.Data.Entity.dll 中的异常但未在用户代码中处理

LINQ to CRM - OR in Where 子句

c# - UWP ProgressBar 和绑定(bind)

wpf - 在 WrapPanel 中同步 WPF 控件宽度