Let us assume that we have a simple UI that has only one state variable. This state is expressed as an enum value, eg. Phase1, Phase2 etc. Depending on which state (phase) the UI is, different UI elements, windows supposed to be visible or hidden.
Here is the code:
public enum Phases { Phase1, Phase2, Phase3 }
public class UIStateModel : DependencyObject
{
public static DependencyProperty CurrentStateProperty =
DependencyProperty.Register("CurrentStateProperty",
typeof(Phases),
typeof(UIStateModel));
public Phases CurrentState
{
get { return (Phases)GetValue(CurrentStateProperty); }
set { SetValue(CurrentStateProperty, value); }
}
public Visibility Window1Visible // Databound to Window1.Visibility
{
get
{
if (this.CurrentState == Phases.Phase1) return Visibility.Visible;
else return Visibility.Hidden;
}
}
public Visibility Window2Visible // Databound to Window2.Visibility
{
get
{
if (this.CurrentState == Phases.Phase2) return Visibility.Visible;
else return Visibility.Hidden;
}
}
...
}
The problem is that data binding with the code above does not work, because WindowXVisible properties are not DependencyProperty-s. Should I turn all properties to DependencyProperty, then I will introduce redundancy into the state management. Besides the extra burden to keep everything in sync, it can even become inconsistent (if I fail to sync well).
What would be the correct way to avoid introducing redundancy in the UI state management, but still leverage the power of databinding facilitated by DependencyProperty-s?
You can use INotifyPropertyChanged. Simply send a change notification for the given WindowXVisible when CurrentState changes (DP has a callback for this).
Bindings can generally listen to changes either via DependencyProperty or INotifyPropertyChanged notifications (which must be sent manually though, unlike with DP).
You can use this tool to generate the notification calls automatically (without increasing complexity of your code by a bit). It handles even such nontrivial cases surprisingly well.
EDIT:
Register this into PropertyMetadata of the CurrentStateProperty.
OnPropertyChanged simply invokes PropertyChanged event with this as sender and the string as property name.
This will cause Window1Visible and Window2Visible bindings to update and get the new values.
By the way, you should try to figure out better names than Window1 and WIndow2.