【发布时间】:2019-10-17 03:33:55
【问题描述】:
我有一个带有复选框的表,它绑定到 ObservableCollection > 集合,我想在其中一个复选框更改我的视图时跟踪此集合的更改。
这是我的代码:
<UserControl.Resources>
<DataTemplate x:Key="DataTemplate_Level2">
<CheckBox IsChecked="{Binding Path=. ,Mode=TwoWay}" Height="40" Width="50" Margin="4,4,4,4"/>
</DataTemplate>
<DataTemplate x:Key="DataTemplate_Level1">
<ItemsControl x:Name="2st" Items="{Binding Path=. ,Mode=TwoWay}" ItemTemplate="{DynamicResource DataTemplate_Level2}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</DataTemplate>
</UserControl.Resources>
<ItemsControl Grid.Column="1" Items="{Binding MyCollection, Mode=TwoWay}" x:Name="lst" ItemTemplate="{DynamicResource DataTemplate_Level1}" Background="Gold"/>
我的 viewModel 属性
public ObservableCollection<ObservableCollection<bool>> MyCollection
{
get
{
return someCollection;
}
set
{
someCollection = value;
RaisePropertyChanged(nameof(MyCollection));
}
}
如何将集合数据更改传递给视图模型?
【问题讨论】:
-
欢迎来到 SO。那么,确切地说,您的问题是哪一个?请记住,SO 不是免费的编码服务。
-
我的问题是如何将集合更改数据传递给视图模型
-
“当其中一个复选框更改我的视图时,我想跟踪此集合的更改”——这是一个规范,而不是一个问题。请提供一个好的minimal reproducible example,清楚地显示您已经尝试过的内容,准确解释该代码的作用,您希望它做什么,以及您无法弄清楚和需要的具体帮助。
-
在属性中,您没有在设置器中将值设置为您的集合。我认为您在 setter 中缺少这一行
someCollection = value
标签: c# wpf binding observablecollection two-way