从WPF中的数据绑定枚举设置可视状态

xoshrz7s  于 2023-10-22  发布在  其他
关注(0)|答案(2)|浏览(205)

大家好,我有一个场景,我想切换4个不同的内容控件。我已经设置了不透明的视觉状态,并根据每个给定的状态折叠(参见代码)。我想做的是将视觉状态绑定到我的View Model的Enum类型的属性。我尝试使用DataStateBehavior,但它需要true/false,这对我不起作用。所以我尝试了DataStateSwitchBehavior,从我所知道的来看,它似乎完全被WPF 4破坏了。有没有更好的办法?如果需要的话,我真的很愿意接受不同的方法,但是我真的很想在等式中保留这个枚举。
编辑:
代码不应该太重要,我只需要知道是否有一个众所周知的解决这个问题的方法。

<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>
bejyjqdl

bejyjqdl1#

您可以通过以下步骤轻松做到这一点:
1.创建一个将在更改枚举属性时引发的事件
1.为枚举的每个值添加GotToStateAction(行为)(在Conditions部分中定义值)以更改Visual状态。当第一次设置属性时,每次值更改时,状态都将设置为正确的状态。
关于GoToStateAction的更多信息可以在这里找到:http://blogs.msdn.com/b/expression/archive/2010/02/22/switching-visual-states-easily-using-gotostateaction.aspx

bxpogfeg

bxpogfeg2#

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

相关问题