How to use VisualStates in ChildWindow?

Can I use VisualStateManager with my subclass of ChildWindow? Calls in VisualStateManager do nothing, and I realized that the only way to achieve this is through manual calls to storyboards. It is so much sloppier and error prone. Has anyone found a way to achieve this?

Updated with example code . To use it, just create a new Silverlight project and call ExampleWindow.ShowWindow () by clicking the button on the main page. You will see the button, although the constructor sets the state that the button should hide.

XAML (ExampleWindow.xaml):

<controls:ChildWindow xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" xmlns:ic="clr-namespace:Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions" x:Class="Client.Windows.ExampleWindow" Title="Example"> <Grid x:Name="LayoutRoot"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="ExampleStateGroup"> <VisualState x:Name="ExampleBaseState"> <Storyboard> <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.0010000" Storyboard.TargetName="button" Storyboard.TargetProperty="(UIElement.Opacity)"> <EasingDoubleKeyFrame KeyTime="00:00:00" Value="0"/> </DoubleAnimationUsingKeyFrames> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <VisualStateManager.CustomVisualStateManager> <ic:ExtendedVisualStateManager/> </VisualStateManager.CustomVisualStateManager> <Button x:Name="button" Content="You Shouldn't See Me" Grid.ColumnSpan="2" Width="150" Height="150" HorizontalAlignment="Center" VerticalAlignment="Center"/> </Grid> </controls:ChildWindow> 

Behind Code (ExampleWindow.xaml.cs):

 using System.Windows; using System.Windows.Controls; namespace Client.Windows { public partial class ExampleWindow : ChildWindow { public ExampleWindow() { InitializeComponent(); VisualStateManager.GoToState( this, "ExampleBaseState", true ); } public static void ShowWindow() { var w = new ExampleWindow(); w.Show(); } } } 
+1
animation silverlight xaml visualstatemanager
source share
4 answers

So far, the only workaround I have found is to put everything that requires visual states in UserControl. User UserControl can have states and successfully switch between them and bind everything that is necessary for ChildWindow through events, methods and properties.

0
source share
+1
source share

I had the same problem when VSM did not work in ChildWindows as Dov. I did this to change my ChildWindow to a UserControl, and then set the UserControl as the content of the shared ChildWindow, before opening it.

 var childWindow = new ChildWindow { Content = someUserControl }; 

Now the problem is that you are losing DialogResult functionality for the ChildWindow class, as your code behavior will be in UserControl. The easiest way to access the DialogResult property for ChildWindow is to simply use the parent property inside the UserControl.

0
source share

The ChildWindow template contains a VisualStateManager that controls the VisualStateGroup "CommonStates" for general animations of child windows. When calling VisualStateManager.GoToState, it searches for states in the ChildWindow Template CommonStates VisualStateGroup. Therefore, you must access the ExtendedVisualStateManager and the VisualStateGroup "ExampleStateGroup" that you created in ChildWindow. The only way I found to do this (something like hackaround) is to create my own GoToState function, which is called to change the state.

 public ExampleWindow() { InitializeComponent(); MyGoToState("ExampleBaseState"); } public void MyGoToState(string stateIn) { VisualState stateShow = null; VisualStateGroup ExampleStateGroup as VisualStateGroup if(ExampleStateGroup != null) { for(int i= 0; i < ExampleStateGroup.States.Count; i++) { stateShow = ExampleStateGroup.States[i] as VisualState; if(stateShow != null) { if(stateShow.Name == stateIn.Trim()) { stateShow.Storyboard.Begin(); } } } } } 
0
source share

All Articles