WPF:VisualStateManager 不工作

发布于 2024-09-04 02:26:12 字数 4422 浏览 10 评论 0原文

我试图弄清楚 VisualStateManager 是如何工作的,但我做不到。

我想这样做:我有一些文本框,用户可以在其中输入他的姓名、地址……并且我正在执行验证,但我不想使用 XAML 中的验证规则绑定来执行此操作。我正在使用 MVVM 模式,我的视图模型的一些代码可以做到这一点。因此,我将文本框的文本绑定到视图模型为我验证的字符串,并且那里有一个布尔变量来判断它是否正确。

我的 XAML:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Custom="http://schemas.microsoft.com/surface/2008"
mc:Ignorable="d"
x:Class="Surface_Bachelorarbeit.NeuerUser"
x:Name="UserControl"
UseLayoutRounding="True"
d:DesignWidth="640" d:DesignHeight="480" Width="800" Height="350">
<Border x:Name="Border1" BorderBrush="Black" BorderThickness="5" CornerRadius="5" DataContext="{Binding Source={StaticResource MainViewModelDataSource}}">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisibilityStates">
            <VisualStateGroup.Transitions>
                <VisualTransition GeneratedDuration="0" To="NameValid">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_valid">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_invalid">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualTransition>
                <VisualTransition GeneratedDuration="0" To="NameInvalid">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_invalid">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_valid">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualTransition>
            </VisualStateGroup.Transitions>
            <VisualState x:Name="NameValid"/>
            <VisualState x:Name="NameInvalid"/>


        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
...
        <Custom:SurfaceTextBox x:Name="vname" TextWrapping="Wrap" Grid.Column="1" Grid.Row="1" Margin="0,1,22,8" FontSize="14.667" SelectionBrush="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" DataContext="{Binding Forename}" Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" LostFocus="vname_LostFocus"/>
...
                <Image x:Name="VN_valid" HorizontalAlignment="Left" Margin="0,1,0,0" Width="22" Source="valid.png" Height="22" VerticalAlignment="Top" d:LayoutOverrides="Height"/>
            <Image x:Name="VN_invalid" HorizontalAlignment="Left" Margin="0,1,0,0" Width="22" Height="22" Source="invalid.png" VerticalAlignment="Top" d:LayoutOverrides="Height"/>
 ...

当用户的焦点离开文本框时,将发生此事件:

    private void vname_LostFocus(object sender, System.Windows.RoutedEventArgs e)
    {
        mvm = ((MainViewModel)FindResource("MainViewModelDataSource"));
        if (mvm.NewCreatedStoreUser.Forename.Valid)
        {
            VisualStateManager.GoToState(this, "NameValid", true);

        }
        else 
        {
            VisualStateManager.GoToState(this, "NameInvalid", true);

        }

     }

我尝试了它并且状态没有更改,尽管事件发生并且它转到正确的 GoToState-Method。什么也没发生。

我也尝试过
ExtendedVisualStateManager.GoToElementState(this.Border1 as FrameworkElement, "NameValid", false);

因为我在这里读到它会有所帮助,但它是一样的......

我做错了什么?

I'm trying to figure out how the VisualStateManager works but I cannot do it.

I want to do this: I have some TextBoxes where the user can enter his name, adress, ... and I'm doing a validation and I didn't want to do that with a validationrules-binding in XAML. I'm using MVVM-pattern and some code of my viewmodel does it. So I'm binding the text of the textbox to a string that the viewmodel validated for me and I have a boolean variable there that tell if it's correct or not.

My XAML:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:Custom="http://schemas.microsoft.com/surface/2008"
mc:Ignorable="d"
x:Class="Surface_Bachelorarbeit.NeuerUser"
x:Name="UserControl"
UseLayoutRounding="True"
d:DesignWidth="640" d:DesignHeight="480" Width="800" Height="350">
<Border x:Name="Border1" BorderBrush="Black" BorderThickness="5" CornerRadius="5" DataContext="{Binding Source={StaticResource MainViewModelDataSource}}">
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisibilityStates">
            <VisualStateGroup.Transitions>
                <VisualTransition GeneratedDuration="0" To="NameValid">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_valid">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_invalid">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualTransition>
                <VisualTransition GeneratedDuration="0" To="NameInvalid">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_invalid">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Visible}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="VN_valid">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{x:Static Visibility.Collapsed}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualTransition>
            </VisualStateGroup.Transitions>
            <VisualState x:Name="NameValid"/>
            <VisualState x:Name="NameInvalid"/>


        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>
...
        <Custom:SurfaceTextBox x:Name="vname" TextWrapping="Wrap" Grid.Column="1" Grid.Row="1" Margin="0,1,22,8" FontSize="14.667" SelectionBrush="{x:Null}" BorderBrush="{x:Null}" Foreground="Black" DataContext="{Binding Forename}" Text="{Binding Value, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" LostFocus="vname_LostFocus"/>
...
                <Image x:Name="VN_valid" HorizontalAlignment="Left" Margin="0,1,0,0" Width="22" Source="valid.png" Height="22" VerticalAlignment="Top" d:LayoutOverrides="Height"/>
            <Image x:Name="VN_invalid" HorizontalAlignment="Left" Margin="0,1,0,0" Width="22" Height="22" Source="invalid.png" VerticalAlignment="Top" d:LayoutOverrides="Height"/>
 ...

When the user's focus leaves the textbox, this event shall happen:

    private void vname_LostFocus(object sender, System.Windows.RoutedEventArgs e)
    {
        mvm = ((MainViewModel)FindResource("MainViewModelDataSource"));
        if (mvm.NewCreatedStoreUser.Forename.Valid)
        {
            VisualStateManager.GoToState(this, "NameValid", true);

        }
        else 
        {
            VisualStateManager.GoToState(this, "NameInvalid", true);

        }

     }

I tried it and the state is not changed, although the event happens and it goes to the right GoToState-Method. Nothing happens.

I also tried
ExtendedVisualStateManager.GoToElementState(this.Border1 as FrameworkElement, "NameValid", false);

because I read here that it would help but it's just the same with that...

What did I do wrong??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

三寸金莲 2024-09-11 02:26:12

我认为 VisualStateManager 不能与 UserControl 一起使用,我不知道为什么。
我用的是控制。

I think VisualStateManager UserControl does not work with, I do not know why.
I used Control.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文