Installing the original VisualState in WPF

When using VisualStateManager in WPF, you may need to switch to VisualState when initializing management. As far as I can tell, there is no way to declare the initial state in Xaml, leaving you with the limited ability to transition to the required state of your code after initialization.

Using the code behind is not always desirable, and if you use Binding to manage your VisualStates, then this is not always possible.

So the question is: how to install the original VisualState in WPF without installing it in the code?

+4
source share
2 answers

Too long to comment

The binding of "should" does not matter. If it works fine with the code for which it is associated with working from xaml, if there is nothing strange in Bindings.

All mixing actions can be seen as a helper tool xaml. The end result: you get some xaml that creates the mixture for you. If you do not want to use the mixture. Just add xaml yourself in VS.

To do this, you can encode GoToStateAction , for example

<Window ... xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" ...> ... <Button x:Name="button" Style="{DynamicResource ButtonStyle1}"> <i:Interaction.Triggers> <i:EventTrigger> <ei:GoToStateAction StateName="YourState" TargetObject="{Binding ElementName=button}" /> </i:EventTrigger> </i:Interaction.Triggers> </Button> 

You will also need the appropriate links in your project.

On a side note, try blend. It has advantages in certain places. You simply would not replace typing xaml directly, but it serves as a good helper. Ignoring him completely, unless he is a meaningless IMO.

+3
source

You can directly associate any control with the visual state during the initializer itself in xaml. To change state, you need to create one dependency property. hope the code below helps you.

 <Grid model:StateManager.VisualStateProperty="{Binding VisibilityState}" > <Grid.RowDefinitions> <RowDefinition Height="48" /> <RowDefinition Height="97" /> <RowDefinition Height="65" /> <RowDefinition Height="297" /> </Grid.RowDefinitions> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="VisibleStateGroup"> <VisualState x:Name="VisibleState"> <Storyboard Duration="0:0:0"> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="myGrid" Storyboard.TargetProperty="(UIElement.Visibility)"> <DiscreteObjectKeyFrame KeyTime="0:0:0"> <DiscreteObjectKeyFrame.Value> <Visibility>Visible</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> <VisualState x:Name="CollapsedState"> <Storyboard Duration="0:0:0"> <ObjectAnimationUsingKeyFrames Storyboard.TargetName="myGrid" Storyboard.TargetProperty="(UIElement.Visibility)"> <DiscreteObjectKeyFrame KeyTime="0:0:0"> <DiscreteObjectKeyFrame.Value> <Visibility>Collapsed</Visibility> </DiscreteObjectKeyFrame.Value> </DiscreteObjectKeyFrame> </ObjectAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid Name="myGrid" Grid.Row="0" Grid.ColumnSpan="2" > <Grid.ColumnDefinitions> <ColumnDefinition Width="383*" /> <ColumnDefinition Width="383*" /> </Grid.ColumnDefinitions> <StackPanel Grid.Column="0" Orientation="Horizontal" Margin="0,0,15,0" HorizontalAlignment="Right" VerticalAlignment="Center"> <Label Content="MyName"></Label> </StackPanel> </Grid> 

Dependency Property Code for Visual State Change

 public class StateManager : DependencyObject { public static string GetVisualStateProperty(DependencyObject obj) { return (string)obj.GetValue(VisualStatePropertyProperty); } public static void SetVisualStateProperty(DependencyObject obj, string value) { obj.SetValue(VisualStatePropertyProperty, value); } public static readonly DependencyProperty VisualStatePropertyProperty = DependencyProperty.RegisterAttached( "VisualStateProperty", typeof(string), typeof(StateManager), new PropertyMetadata((s, e) => { var propertyName = (string)e.NewValue; var ctrl = s as Grid; if (ctrl == null) throw new InvalidOperationException("This attached property only supports types derived from FrameworkElement."); var transitionWorked = System.Windows.VisualStateManager.GoToElementState(ctrl, (string)e.NewValue, true); //MessageBox.Show(transitionWorked.ToString()); })); } 
0
source

Source: https://habr.com/ru/post/1484776/


All Articles