gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 10:26:20 25 4
gpt4 key购买 nike

我想将 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com