4

大家好,我有一个场景,我想切换 4 个不同内容控件的可见性。我设置了不透明度的视觉状态,并根据每个给定的状态折叠(参见代码。)我想做的是将视觉状态绑定到我的 Enum 类型的视图模型的属性。我尝试使用 DataStateBehavior,但它需要 true/false,这对我不起作用。所以我尝试了 DataStateSwitchBehavior,据我所知,它似乎完全被 WPF4 破坏了。有没有更好的方法来做到这一点?如果需要,我真的对不同的方法持开放态度,但我真的很想将这个枚举保留在等式中。

编辑:

代码不应该太重要,我只需要知道这个问题是否有众所周知的解决方案。

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:Custom="http://schemas.microsoft.com/expression/2010/interactivity" 
         xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions" 
         xmlns:ee="http://schemas.microsoft.com/expression/2010/effects" 
         xmlns:customBehaviors="clr-namespace:SEL.MfgTestDev.ESS.Behaviors"
         x:Class="SEL.MfgTestDev.ESS.View.PresenterControl" 
         mc:Ignorable="d" 
         d:DesignHeight="624" 
         d:DesignWidth="1104" 
         d:DataContext="{Binding ApplicationViewModel, Mode=OneWay, Source={StaticResource Locator}}">
<Grid>
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Layout/TerminalViewTemplate.xaml"/>
                <ResourceDictionary Source="Layout/DebugViewTemplate.xaml"/>
                <ResourceDictionary Source="Layout/ProgressViewTemplate.xaml"/>
                <ResourceDictionary Source="Layout/LoadoutViewTemplate.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
    <Custom:Interaction.Behaviors>
        <customBehaviors:DataStateSwitchBehavior Binding="{Binding ApplicationViewState}">
            <customBehaviors:DataStateSwitchCase State="LoadoutState" Value="Loadout"/>
        </customBehaviors:DataStateSwitchBehavior>
    </Custom:Interaction.Behaviors>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="ApplicationStates" ei:ExtendedVisualStateManager.UseFluidLayout="True">
            <VisualStateGroup.Transitions>
                <VisualTransition GeneratedDuration="0:0:1">
                    <VisualTransition.GeneratedEasingFunction>
                        <SineEase EasingMode="EaseInOut"/>
                    </VisualTransition.GeneratedEasingFunction>
                    <ei:ExtendedVisualStateManager.TransitionEffect>
                        <ee:SmoothSwirlGridTransitionEffect/>
                    </ei:ExtendedVisualStateManager.TransitionEffect>
                </VisualTransition>
            </VisualStateGroup.Transitions>
            <VisualState x:Name="LoadoutState">
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="LoadoutPage">
                        <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="LoadoutPage">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
            <VisualState x:Name="ProgressState">
                <Storyboard>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="ProgressPage">
                        <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="ProgressPage">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
            <VisualState x:Name="DebugState">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="DebugPage">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="DebugPage">
                        <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
            <VisualState x:Name="TerminalState">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="TerminalPage">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                    </ObjectAnimationUsingKeyFrames>
                    <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="TerminalPage">
                        <EasingDoubleKeyFrame KeyTime="0" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
    <ContentControl x:Name="LoadoutPage" ContentTemplate="{StaticResource LoadoutViewTemplate}" Opacity="0" Content="{Binding}" Visibility="Collapsed"/>
    <ContentControl x:Name="ProgressPage" ContentTemplate="{StaticResource ProgressViewTemplate}" Opacity="0" Content="{Binding}" Visibility="Collapsed"/>
    <ContentControl x:Name="DebugPage" ContentTemplate="{StaticResource DebugViewTemplate}" Opacity="0" Content="{Binding}" Visibility="Collapsed"/>
    <ContentControl x:Name="TerminalPage" ContentTemplate="{StaticResource TerminalViewTemplate}" Opacity="0" Content="{Binding}" Visibility="Collapsed"/>
    <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding ApplicationViewState}">
        <TextBlock.Background>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="Black" Offset="0"/>
                <GradientStop Color="White" Offset="1"/>
            </LinearGradientBrush>
        </TextBlock.Background>
    </TextBlock>
</Grid>

4

2 回答 2

3

您可以按照以下步骤轻松完成此操作:

  1. 创建一个将在枚举属性更改时引发的事件
  2. 为枚举的每个值添加 GotToStateAction(行为)(在条件部分中定义值)以更改视觉状态。当第一次设置属性时,每次值更改时,状态都会被设置为正确的状态。

可以在此处找到有关 GoToStateAction 的更多信息:http: //blogs.msdn.com/b/expression/archive/2010/02/22/switching-visual-states-easily-using-gotostateaction.aspx

于 2010-08-03T10:09:40.463 回答
0

使用 valueconverter(实现 IValueConverter 的类)将枚举转换为 Visibility。

于 2010-06-16T19:28:54.920 回答