CollectionChanged = null帮助理解 [英] CollectionChanged = null Help understand

查看:70
本文介绍了CollectionChanged = null帮助理解的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码

名称空间WpfApplication4

{

&NBSP; &NBSP;公共类WPFConcurrentDictionary< TKey,TValue> :ConcurrentDictionary< TKey,TValue> ;, INotifyCollectionChanged

  &NBSP; {&NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; public void OnPropertyChanged()

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; if(CollectionChanged!= null)

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; CollectionChanged(this,new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));

  &NBSP; &NBSP; &NBSP; }&NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP;公共事件NotifyCollectionChangedEventHandler CollectionChanged;

  &NBSP; }


  &NBSP;公共课Maksim

  &NBSP; {

  &NBSP; &NBSP; &NBSP; public int Id {get;组; }¥b $ b  &NBSP; &NBSP; &NBSP; public string Name {get;组; }¥b $ b  &NBSP; &NBSP; &NBSP; public string LastName {get;组; }¥b $ b  &NBSP; }  



  &NBSP; public partial class MainWindow:Window

  &NBSP; {

  &NBSP; &NBSP; &NBSP; public MainWindow()

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; InitializeComponent();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; DataContext = new viewModel();

  &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; }


  &NBSP;公共类viewModel

  &NBSP; {&NBSP; &NBSP; &NBSP;   

  &NBSP; &NBSP; &NBSP; private void Da()

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; OldBase = new WPFConcurrentDictionary< string,Maksim>();

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; OldBase.TryAdd(" 001",new Maksim {Id = 1,LastName =" Maksim2",Name =" Maksim"});

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; OldBase.TryAdd(" 002",new Maksim {Id = 2,LastName =" Maksim6",Name =" Maksim5"});

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; OldBase.OnPropertyChanged();

  &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; public WPFConcurrentDictionary< string,Maksim> oldBase;

  &NBSP; &NBSP; &NBSP; public WPFConcurrentDictionary< string,Maksim> OldBase

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;得到
  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;返回oldBase;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP;设为

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; oldBase = value;

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; OldBase.OnPropertyChanged();



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; &NBSP; &NBSP; }


  &NBSP; &NBSP; &NBSP; public viewModel()

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; OldBase = new WPFConcurrentDictionary< string,Maksim>();

  &NBSP; &NBSP; &NBSP; }¥b $ b  &NBSP; }
}

namespace WpfApplication4
{
    public class WPFConcurrentDictionary<TKey, TValue> : ConcurrentDictionary<TKey, TValue>, INotifyCollectionChanged
    {      
        public void OnPropertyChanged()
        {
            if (CollectionChanged != null)
                CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }      
        public event NotifyCollectionChangedEventHandler CollectionChanged;
    }

    public class Maksim
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string LastName { get; set; }
    }  

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new viewModel();
        }
    }

    public class viewModel
    {        
        private void Da()
        {
            OldBase = new WPFConcurrentDictionary<string, Maksim>();
            OldBase.TryAdd("001", new Maksim { Id = 1, LastName = "Maksim2", Name = "Maksim" });
            OldBase.TryAdd("002", new Maksim { Id = 2, LastName = "Maksim6", Name = "Maksim5" });
            OldBase.OnPropertyChanged();
        }

        public WPFConcurrentDictionary<string, Maksim> oldBase;
        public WPFConcurrentDictionary<string, Maksim> OldBase
        {
            get
            {
                return oldBase;
            }
            set
            {
                oldBase = value;
                OldBase.OnPropertyChanged();

            }
        }

        public viewModel()
        {
            OldBase = new WPFConcurrentDictionary<string, Maksim>();
        }
    }
}

AND XAML 

AND XAML 

< Window x :Class =" WpfApplication4.MainWindow"

  &NBSP; &NBSP; &NBSP; xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  &NBSP; &NBSP; &NBSP; xmlns:x =" http://schemas.microsoft.com/winfx/2006/xaml"

  &NBSP; &NBSP; &NBSP; xmlns:d =" http://schemas.microsoft.com/expression/blend/2008"

  &NBSP; &NBSP; &NBSP; xmlns:mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"

  &NBSP; &NBSP; &NBSP; xmlns:local =" clr-namespace:WpfApplication4"

  &NBSP; &NBSP; &NBSP; mc:Ignorable =" d"

  &NBSP; &NBSP; &NBSP;标题= QUOT;主窗口"高度= QUOT; 350"宽度="525">

  &NBSP; < StackPanel>

  &NBSP; &NBSP; &NBSP; < ListBox Name =" lb" ItemsSource =" {Binding OldBase}"  >

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < ListBox.ItemTemplate>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < DataTemplate>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < TextBlock Text =" {Binding Value.Name}" />

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < / DataTemplate>

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; < /ListBox.ItemTemplate>&NBSP;  

  &NBSP; &NBSP; &NBSP; < / ListBox>

  &NBSP; &NBSP; &NBSP; < Button Command =" {Binding D}"  &NBSP; >< / Button>

  &NBSP; < / StackPanel>

< / Window>

<Window x:Class="WpfApplication4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApplication4"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <ListBox Name="lb" ItemsSource="{Binding OldBase}"  >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Value.Name}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>   
        </ListBox>
        <Button Command="{Binding D}"    ></Button>
    </StackPanel>
</Window>

如果我评论行

   private void Da()

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP;的&NBSP; // OldBase = new WPFConcurrentDictionary< string,Maksim>();

   private void Da()
        {
            //OldBase = new WPFConcurrentDictionary<string, Maksim>();

一切正常工作

但是如果写

  private void Da()

  &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP;&的&NBSP; OldBase = new WPFConcurrentDictionary< string,Maksim>();

  private void Da()
        {
            OldBase = new WPFConcurrentDictionary<string, Maksim>();

代码不起作用  因为
CollectionChanged = null

Code Not Work because CollectionChanged =null

帮助我了解  究竟发生了什么

Help me understand what exactly is happening

推荐答案

当您创建新的WPFConcurrentDictionary时,必须添加处理程序,因为它是一个新对象。

When you create a new WPFConcurrentDictionary the handler would have to added since it is a new object.

我会清除集合而不是创建一个新集合,然后旧处理程序仍然存在。

I would clear the collection rather than create a new one and then the old handler will still be in place.


这篇关于CollectionChanged = null帮助理解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆