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

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

问题描述

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

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
    }
}

这个 ObservableCollection 绑定到一个 ListBox.

This ObservableCollection is bound to a ListBox.

我在 GUI 中添加了一个按钮来重新加载变量,点击后它会调用 Reload() 过程.

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

然而,ListBox 的内容不会改变,我无法在调用 Reload() 后向列表添加更多项目.

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.

列表框 XAML :

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

我也尝试使用 PropertyChanged 作为 UpdateSource 触发器并设置了大部分模式.

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(@"SYSTEMCurrentControlSetControlSession ManagerEnvironment");

        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();
    }

推荐答案

您需要为您的 VariableVieWModel 实现 INotifyPropertyChanged 以刷新您的目标对象绑定.你只需要这样做 -

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天全站免登陆