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