Reputation: 3682
I am working with C# using the MVVM pattern. I have two WPF windows, each with a view model. Basically I need to pass a property of the main view model to the 'child' view model. At the minute, I do this by setting a private variable equal to the new view model in the main view model's constructor, and in doing so passing the property in the constructor of the child view model.
However, there is a dependency property linked to the property as it used as a binding for the selected item in a combobox. Therefore, it is likely to change after the child view model is initialized, but by passing the property in the constructor, the change is not made in my child view model.
So, is there anyway for me to pass the property into the constructor and have it change in the child view model when it does in the main view model? Or would I have to create a property in the child view model which is updated everytime the property in the main view model is set?
Hope that makes sense.
Edit Inside my main view model, I declare the following:
public readonly DependencyProperty CurrentDatabaseManagedProperty = DependencyProperty.Register("CurrentDatabaseManaged", typeof(DatabaseInfo), typeof(MainViewModel));
public DatabaseInfo CurrentDatabaseManaged {
get { return (DatabaseInfo)GetValue(CurrentDatabaseManagedProperty); }
set { SetValue(CurrentDatabaseManagedProperty, value); }
}
public DatabaseInfo CurrentDatabaseManagedSelection {
get { return CurrentDatabaseManaged; }
set {
if (CurrentDatabaseManaged != null &&
(String.Equals(value.Name, CurrentDatabaseManaged.Name, StringComparison.OrdinalIgnoreCase))) return;
CurrentDatabaseManaged = (value.IsUsable ? value : dbm.ReadDatabase(value.FileName));
}
}
Where CurrentDatabaseManagedSelection
is the SelectedItem of the combobox. In the constructor of the main view model, I have the following:
_DatabaseVM = new ChildViewModel(CurrentDatabaseManaged);
And the constructor of ChildViewModel
looks like this:
public ChildViewModel( DatabaseInfo SelectedDatabase)
{
if (SelectedDatabase != null)
_SelectedDatabase = SelectedDatabase;
}
}
Basically I would like _SelectedDatabase
to be updated whenever CurrentDatabaseManagedSelection
is.
Upvotes: 0
Views: 704
Reputation: 56536
It looks like you want to bind to the CurrentDatabaseManagedSelection
property. The simplest way to simulate this is to add this to the setter of that property:
_DatabaseVM._SelectedDatabase = value;
To do it with actual bindings, you'd need to
ChildViewModel._SelectedDatabase
a dependency property,MainViewModel
implement INotifyPropertyChanged
, andPropertyChanged
event in the setter of CurrentDatabaseManagedSelection
.ChildViewModel
extend DependencyObject
instead of just setting the property, set a binding, e.g.
BindingOperations.SetBinding(this, _SelectedDatabaseProperty,
new Binding("CurrentDatabaseManagedSelection") { Source = mainViewModel });
Upvotes: 0
Reputation:
You have to change the value later, after bindings are set up in the UI.
Use the Dispatcher.BeginInvoke method to put off updating the property until later on.
public MyClass(object someValue)
{
Dispatcher.BeginInvoke(
(Action)(() => Property = someValue), // the cast may not be required
DispatcherPriority.ApplicationIdle); // runs after everything is loaded
}
Upvotes: 1