Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
476 views
in Technique[技术] by (71.8m points)

c# - Listbox bound to an ObservableCollection doesn't update

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(@"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();
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You haven't modified the ObservableCollection, only replaced it. You should implement INotifyPropertyChanged and call PropertyChanged in the Variables property setter.

BTW it's common in MVVM implementations to have a base class for your ViewModel:

class VariableViewModel : ViewModelBase

and implement common functionality such as INotifyPropertyChanged in the base class to avoid duplication of code.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...