绑定到一个ObservableCollection列表框不更新 [英] Listbox bound to an ObservableCollection doesn't update

查看:385
本文介绍了绑定到一个ObservableCollection列表框不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的ObservableCollection来存储Windows环境变量。

 类VariableVieWModel 
{
的ObservableCollection< VariableModel>瓦尔;

公众的ObservableCollection< VariableModel>变量
{
得到
{
返回瓦尔;
}

{
瓦尔=价值;
}
}

公共VariableViewModel()
{
刷新();
}

公共无效刷新()
{
//代码检索瓦尔
}
}

本的ObservableCollection绑定到一个列表框。



我添加了一个按钮在GUI中重新加载whichi上点击,它会调用刷新()过程中的变量。



ListBox的内容,但是,并没有改变,我无法项目添加了到。名单AFER调用刷新()



在构造函数中一切正常。



ListBox的XAML:

 < ListBox的X:名称=lbVariablesGrid.Column =0利润=0,0,5,0 的ItemsSource ={绑定源= {StaticResource的variablesViewModel},路径=变量}IsSynchronizedWithCurrentItem =真> 



我试着用同样的PropertyChanged作为UpdateSource触发,并设置大部分的模式。

 公共无效刷新()
{
瓦尔=新的ObservableCollection< VariableModel>();

的RegistryKey systemVarKey = Registry.LocalMachine.OpenSubKey(@SYSTEM\CurrentControlSet\Control\Session Manager\Environment);

的String [] = systemVars systemVarKey.GetValueNames();

的foreach(在systemVars字符串变量)
{
vars.Add(新VariableModel()
{
名称= VAR,
值=(systemVarKey.GetValue(VAR,空,RegistryValueOptions.DoNotExpandEnvironmentNames)作为字符串).Split(';')。&了ToList LT;串>(),
目标= EnvironmentVariableTarget.Machine
});
}
systemVarKey.Close();

的RegistryKey userVarKey = Registry.CurrentUser.OpenSubKey(@环境);

的String [] =的UserVars userVarKey.GetValueNames();

的foreach(在字符串的UserVars VAR)
{
vars.Add(新VariableModel()
{
名称= VAR,
值=(userVarKey.GetValue(VAR,空,RegistryValueOptions.DoNotExpandEnvironmentNames)作为字符串).Split(';')。&了ToList LT;串>(),
目标= EnvironmentVariableTarget.User
});
}
userVarKey.Close();
}


解决方案

您需要执行 INotifyPropertyChanged的为您VariableVieWModel刷新你的目标对象绑定。你只需要做到这一点的方式 -

 类VariableVieWModel:INotifyPropertyChanged的
{。

公众的ObservableCollection< VariableModel>变量
{
得到
{
返回瓦尔;
}

{
如果(瓦尔=价值!)
{
瓦尔=价值;
OnPropertyChanged(变量);
}
}
}

公共事件PropertyChangedEventHandler的PropertyChanged;
保护无效OnPropertyChanged(字符串名称)
{
PropertyChangedEventHandler处理器=的PropertyChanged;
如果(处理!= NULL)
{
处理器(这一点,新PropertyChangedEventArgs(名));
}
}
}


I am using a ObservableCollection to store the Environment Variables of Windows.

class VariableVieWModel
{
    ObservableCollection<VariableModel> vars;

    public ObservableCollection<VariableModel> Variables
    {
        get
        {
            return vars;
        }
        set
        {
            vars = value;
        }
    }

    public VariableViewModel() 
    {
        Reload();
    }

    public void Reload()
    {
    // Code to retrieve vars
    }
}

This ObservableCollection is bound to a ListBox.

I have added a button in the GUI to reload the Variables whichi, on click, it calls the Reload() procedure.

ListBox content, however, does not change and I cannot add anymore items to the list afer calling Reload().

Under the constructor everything is working fine.

ListBox XAML :

<ListBox x:Name="lbVariables" Grid.Column="0" Margin="0,0,5,0" ItemsSource="{Binding Source={StaticResource variablesViewModel}, Path=Variables}" IsSynchronizedWithCurrentItem="True"> 

I tried using also PropertyChanged as UpdateSource trigger and set most of the Modes.

public void Reload()
    {
        vars = new ObservableCollection<VariableModel>();

        RegistryKey systemVarKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Session Manager\Environment");

        string[] systemVars = systemVarKey.GetValueNames();

        foreach (string var in systemVars)
        {
            vars.Add(new VariableModel()
            {
                Name = var,
                Values = (systemVarKey.GetValue(var, null, RegistryValueOptions.DoNotExpandEnvironmentNames) as string).Split(';').ToList<string>(),
                Target = EnvironmentVariableTarget.Machine
            });
        } 
        systemVarKey.Close();

        RegistryKey userVarKey = Registry.CurrentUser.OpenSubKey(@"Environment");

        string[] userVars = userVarKey.GetValueNames();

        foreach (string var in userVars)
        {
            vars.Add(new VariableModel()
            {
                Name = var,
                Values = (userVarKey.GetValue(var, null, RegistryValueOptions.DoNotExpandEnvironmentNames) as string).Split(';').ToList<string>(),
                Target = EnvironmentVariableTarget.User
            });
        }
        userVarKey.Close();
    }

解决方案

You need to implement INotifyPropertyChanged for your VariableVieWModel to refresh your target object bindings. You just have to do this way -

class VariableVieWModel : INotifyPropertyChanged
    { .
      .
            public ObservableCollection<VariableModel> Variables
            {
               get
               {
                  return vars;
               }
               set
               {
                  if(vars!=value)
                  {
                      vars = value;
                      OnPropertyChanged("Variables");
                  }
               }
            }

            public event PropertyChangedEventHandler PropertyChanged;      
            protected void OnPropertyChanged(string name)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(name));
                }
            } 
    }

这篇关于绑定到一个ObservableCollection列表框不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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