<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="00:00:00.1000000"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal">
</VisualState>
<VisualState x:Name="Focused">
</VisualState>
<VisualState x:Name="Pressed">
</VisualState>
<VisualState x:Name="MouseOver">
</VisualState>
<VisualState x:Name="Selected">
</VisualState>
</VisualStateGroup>
I have xaml as above i am programmatically applying Selected state by using VisualStateManager.GoToState and after applying it again i do mouse over it then it applies MouseOver styles How do i prevent overriding of style?
if you want separate viewstates you have to separate them into separate visualstategroups. Any Visual state in the same visualstate group can override any other visual state in the same group.
So if you dont want mouseover to override selected you have to put them in separate groups.
Related
I'm trying to do my own Slider Style in XAML, but i don't get it quite right. The VisualState x:Name="Pressed" is not being triggered.
...
<ControlTemplate TargetType="Slider">
<Grid>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup Name="CommonStates">
<VisualState Name="Normal" />
<VisualState Name="MouseOver">
...
</VisualState>
<VisualState Name="Pressed">
<Storyboard>
<ColorAnimation Storyboard.TargetName="RepeatButtonValueBrush" Storyboard.TargetProperty="Color" To="{StaticResource PressedValueColor}" Duration="0"/>
<ColorAnimation Storyboard.TargetName="RepeatButtonRestBrush" Storyboard.TargetProperty="Color" To="{StaticResource PressedTrackColor}" Duration="0"/>
</Storyboard>
</VisualState>
<VisualState Name="Disabled">
...
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
...
</Grid>
</ControlTemplate>
...
What am I doing wrong?
Thanks
You need to call the VisualStateManager.GoToState method to change visual states. See this VisualStateManager and Triggers article.
I created a templated hyperlinkbutton in wpf (windows 8 metro app):
<ControlTemplate TargetType="HyperlinkButton">
<HyperlinkButton>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition To="PointerOver" GeneratedDuration="0:0:0.1"/>
</VisualStateGroup.Transitions>
</VisualStateGroup>
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="HyperlinkForegroundBrush" Storyboard.TargetProperty="Color" To="#FF011751"/>
</Storyboard>
</VisualState>
</VisualStateManager.VisualStateGroups>
<ContentPresenter Content={TemplateBinding Content}>
<ContentPresenter.Foreground>
<SolidColorBrush x:Name="HyperlinkForegroundBrush" Color="3FFB20404"/>
</ContentPresenter.Foreground>
</ContentPresenter>
</HyperlinkButton>
and this is the hyperlinkbutton::
<HyerlinkButton Style={StaticResource MainPageLinkStyle} x:Name="MoreDetailsHyperlinkButton" Content="More..." Click="MoreDetailsHyperlinkButton_Click"/>
The style MainPageLinkStyle refers to the above style mentioned.
Problem: The click of the hyperlinkbutton doesn't gets executed.
Please help.
Thanks in advance.
EDIT:
Instead of hyperlinkbutton's click event, when I use PointerPressed event, the mouse right click triggers this event,but not the mouse left click....Seems strange to me.
I found the solution. Here's the modified markup:
<ControlTemplate TargetType="HyperlinkButton">
<**Border**>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<VisualTransition To="PointerOver" GeneratedDuration="0:0:0.1"/>
</VisualStateGroup.Transitions>
</VisualStateGroup>
<VisualState x:Name="Normal"/>
<VisualState x:Name="PointerOver">
<Storyboard>
<ColorAnimation Storyboard.TargetName="HyperlinkForegroundBrush" Storyboard.TargetProperty="Color" To="#FF011751"/>
</Storyboard>
</VisualState>
</VisualStateManager.VisualStateGroups>
<ContentPresenter Content={TemplateBinding Content}>
<ContentPresenter.Foreground>
<SolidColorBrush x:Name="HyperlinkForegroundBrush" Color="3FFB20404"/>
</ContentPresenter.Foreground>
</ContentPresenter>
</**Border**>
As highlighted in the markup, I need to use Border class instead of Hyperlink. I am not able to understand why, but this seems to be a possible solution working for me.
I have my visual states defined in UserControl.Resources. This is the sample :-
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="AlbumDetailsStates">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:0.3">
<VisualTransition.GeneratedEasingFunction>
<CircleEase EasingMode="EaseIn"/>
</VisualTransition.GeneratedEasingFunction>
</VisualTransition>
</VisualStateGroup.Transitions>
<VisualState x:Name="HideState"/>
<VisualState x:Name="ShowState">
<Storyboard>
<DoubleAnimation Duration="0" To="250" Storyboard.TargetProperty="(FrameworkElement.Width)" Storyboard.TargetName="border" d:IsOptimized="True"/>
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(TextBlock.TextWrapping)" Storyboard.TargetName="textBlock">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<TextWrapping>NoWrap</TextWrapping>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="border" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<VisualStateManager.CustomVisualStateManager>
<ei:ExtendedVisualStateManager/>
</VisualStateManager.CustomVisualStateManager>
Now i want to change the states from within my itemtemplate of listbox. I am using GoToStateAction behavior of Blend. However it is not able to find my states. But if i try to change states from outside itemtemplate it is available. It is so annoying. How do i workaround this issue?
Thanks in advance :)
Have you tried defining the visual states as part of your root layout container instead of in the resources section?
I'm trying to familiarize myself with WPF for an upcoming project. I'm having problem animating margins between states. Using Blend I've obtained the following sample XAML. If I trigger the state "Large" from codebehind, the rectangle suddenly expands after 1 second, instead of "easing" into the expansion, which is the desired effect.
<Grid x:Name="LayoutRoot">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="VisualStateGroup">
<VisualStateGroup.Transitions>
<VisualTransition GeneratedDuration="0:0:1"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Large">
<Storyboard>
<ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="rectangle">
<EasingThicknessKeyFrame KeyTime="0" Value="64,135,47,191"/>
</ThicknessAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="rectangle" Fill="#FFF4F4F5" Margin="428,135,47,191" Stroke="Black"/>
</Grid>
Thanks in advance!
I think it is because you only have one keyframe set to 0 in your animation.
try this :
<Grid x:Name="LayoutRoot">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualStateGroup.Transitions>
<!--Take one second to transition to the Large state.-->
<VisualTransition To="Large" GeneratedDuration="0:0:1"/>
</VisualStateGroup.Transitions>
<VisualState x:Name="Normal" />
<!--Change the Margin to "64,135,47,191" when the states gets to "Large"-->
<VisualState x:Name="Large">
<Storyboard>
<ThicknessAnimation Storyboard.TargetName="rectangle" Storyboard.TargetProperty="Margin" To="64,135,47,191" />
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
<Rectangle x:Name="rectangle" Margin="428,135,47,191" Fill="#FFF4F4F5" Stroke="Black"/>
</Grid>
edit: I was not very pleased with my first shot, so here is a better way of doing this.
Preferably without using backend code? I'm looking for the cleanest solution for doing a fade in fade out hover button using 2 images. Here is what I have so far
Edit:
I got this to partially work.. problem is now that the mouseout seems abrupt whereas the mouseover seems fine, what am I doing wrong?
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal" />
<VisualState x:Name="MouseOver">
<Storyboard>
<DoubleAnimation BeginTime="0:0:0" Duration="0:0:1" To="1"
Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="mouseOverImage" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
<VisualState x:Name="MouseOut">
<Storyboard>
<DoubleAnimation BeginTime="0:0:0" Duration="0:0:1" To="0"
Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="mouseOverImage" d:IsOptimized="True"/>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed"/>
<VisualState x:Name="Disabled"/>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused"/>
<VisualState x:Name="Unfocused"/>
</VisualStateGroup>
Found out there is no default visual state called mouseout. the normal state will work as the mouseout or mouseleave.
<Storyboard>
<DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.5" From="0" To="1"
Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="mouseOverImage" />
</Storyboard>
</VisualState>
<VisualState x:Name="Normal">
<Storyboard>
<DoubleAnimation BeginTime="0:0:0" Duration="0:0:0.5" From="1" To="0"
Storyboard.TargetProperty="(UIElement.Opacity)"
Storyboard.TargetName="mouseOverImage" />
</Storyboard>