Benutzer:MovGP0/WPF/Visual State Manager

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


Visual State Manager

[Bearbeiten | Quelltext bearbeiten]
<VisualStateManager.VisualStateGroups>
    <VisualStateGroup Name="CommonStates">
        <VisualState Name="Normal" />
        <VisualState Name="MouseOver">
            <Storyboard>
                <ColorAnimation Storyboard.TargetProperty="Background.(SolidColorBrush)" 
                                Storyboard.TargetName="textBox" 
                                To="Green" />
            </Storyboard>
        </VisualState>
    </VisualStateGroup>
</VisualStateManager.VisualStateGroups>
[TemplateVisualState(Name="Normal", GroupName="CommonStates")]
[TemplateVisualState(Name="MouseOver", GroupName="CommonStates")]
public sealed class MyControl : Control
{
    private override void OnMouseEnter(MouseEnterEventArgs e)
    {
        base.OnMouseEnter(e);
        UpdateVisualState(true);
    }

    private override void OnMouseLeave(MouseEnterEventArgs e)
    {
        base.OnMouseEnter(e);
        UpdateVisualState(true);
    }

    private void UpdateVisualState(bool useTransitions)
    {
        if(IsMouseOver)
        {
            VisualStateManager.GoToState(this, "MouseOver", useTransitions);
        }
        else
        {
            VisualStateManager.GoToState(this, "Normal", useTransitions);
        }
    }
}

Adapt to screen size

[Bearbeiten | Quelltext bearbeiten]
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <!-- wider than 720 px -->
            <VisualState x:Name="wideView">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="720" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="best.(RelativePanel.RightOf)" Value="free"/>
                    <Setter Target="best.(RelativePanel.AlignTopWidth)" Value="free"/>
                </VisualState.Setters>
            </VisualState>

            <!-- narrower than 720 px -->
            <VisualState x:Name="narrowView">
                <VisualState.Setters>
                    <Setter Target="best.(RelativePanel.Below)" Value="paid"/>
                    <Setter Target="best.(RelativePanel.AlignLeftWithPanel)" Value="true"/>
                </VisualState.Setters>
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0" />
                </VisualState.StateTriggers>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    ...
</Grid>

Boolean Property

[Bearbeiten | Quelltext bearbeiten]
<UserControl ... x:Name="This">
    <Control.Style>
        <Style TargetType="Control">
            <Setter Property="Foreground">
                <Setter.Value>
                    <SolidColorBrush Color="#FF222222"/>
                </Setter.Value>
            </Setter>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Control">
                        ...
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Control.Style>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup>
            <VisualState x:Name="flashing">
                <VisualState.StateTriggers>
                    <StateTrigger IsActive="{Binding ElementName=This, Path=IsEnabled}" />
                </VisualState.StateTriggers>
                <VisualState.Storyboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" From="0.0" To="1.0" RepeatBehavior="Forever" AutoReverse="True" Duration="0:0:0.5">
                            <DoubleAnimation.EasingFunction>
                                <SineEase EasingMode="EaseInOut"/>
                            </DoubleAnimation.EasingFunction>
                        </DoubleAnimation>
                    </Storyboard>
                </VisualState.Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
</UserControl>