Note that there are some explanatory texts on larger screens.

plurals
  1. PODependencyProperty VS redundancy-free state management
    text
    copied!<p>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.</p> <p>Here is the code:</p> <pre><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; } } ... } </code></pre> <p>The problem is that <strong>data binding with the code above does not work</strong>, 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). </p> <p>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?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload