Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One way I have solved this problem is to make a behavior you can add to your control to put it into an initial visual state upon loading. Here is a simple example:</p> <pre><code>public class InitialVisualStateBehavior : Behavior&lt;Control&gt; { public static readonly DependencyProperty InitialStateProperty = DependencyProperty.Register( "InitialState", typeof(string), typeof(InitialVisualStateBehavior), null); public string InitialState { get { return (string)GetValue(InitialStateProperty); } set { SetValue(InitialStateProperty, value); } } protected override void OnAttached() { base.OnAttached(); if (this.AssociatedObject != null) { this.AssociatedObject.Loaded += new RoutedEventHandler(AssociatedObject_Loaded); } } protected override void OnDetaching() { base.OnDetaching(); if (this.AssociatedObject != null) { this.AssociatedObject.Loaded -= AssociatedObject_Loaded; } } private void AssociatedObject_Loaded(object sender, RoutedEventArgs e) { VisualStateManager.GoToState(this.AssociatedObject, this.InitialState, false); } } </code></pre> <p>You would then just add that behavior to the UserControl-level in XAML:</p> <pre><code>&lt;i:Interaction.Behaviors&gt; &lt;myi:InitialVisualStateBehavior InitialState="SelectedVisualState" /&gt; &lt;/i:Interaction.Behaviors&gt; </code></pre> <p>You could also easily modify this to accept a comma separated list of initial states which you could then split and loop through if you needed to put the control in a bunch of different mutually exclusive states after loading.</p> <p>This might also be refactored into a TriggerAction that you could just trigger off of the Loaded event of the control, I'm not sure which way would be cleaner.</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