Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 双向集合绑定_C#_Wpf_Binding_Observablecollection_Two Way - Fatal编程技术网

C# 双向集合绑定

C# 双向集合绑定,c#,wpf,binding,observablecollection,two-way,C#,Wpf,Binding,Observablecollection,Two Way,我有一个表,其中包含绑定到ObservableCollection>collection的复选框,我希望在其中一个复选框更改我的视图时跟踪对此集合的更改 这是我的代码: <UserControl.Resources> <DataTemplate x:Key="DataTemplate_Level2"> <CheckBox IsChecked="{Binding Path=. ,Mode=TwoWay}" Height="40" Width="50" Margin

我有一个表,其中包含绑定到ObservableCollection>collection的复选框,我希望在其中一个复选框更改我的视图时跟踪对此集合的更改

这是我的代码:

<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));
        }
 }
public observeablecollection MyCollection
{
得到
{
归还部分藏品;
}
设置
{
someCollection=价值;
RaisePropertyChanged(名称(MyCollection));
}
}


如何将集合数据更改传递给视图模型?

您需要声明一个新类,该类将成为checkbox的viewmodel,其属性类型为book,并调用适当的RaisePropertyChanged。MyCollection必须是该类实例集合的集合,而不是bool

public class CheckboxViewModel
{
   private bool _checkboxValue;

   public bool CheckboxValue
   {
      get
      {
         return _checkboxValue;
      }
      set
      {
         _checkboxValue = value;
         RaisePropertyChanged(nameof(CheckboxValue));
      }
   }
}
确保在复选框视图中对该属性具有双向绑定


顺便说一句-在您的示例中,MyCollection的setter处更改的RaiseProperty引发属性名称错误的事件。

欢迎使用SO。那么,确切地说,你的问题是哪一个?请记住,SO不是免费的编码服务。我的问题是如何将集合更改数据传递给视图模型“当其中一个复选框更改我的视图时,我希望跟踪此集合的更改”——这是一个规范,而不是一个问题。请提供一个好的示例,清楚地显示您已经尝试了什么,精确地解释该代码的作用,您希望它做什么,以及您无法理解和需要帮助的具体内容。在属性中,您没有在setter中设置集合的值。我认为您在setter中缺少这一行
someCollection=value