WPF:VisualStateManager无法正常工作

时间:2010-06-01 07:41:47

标签: wpf expression-blend visualstatemanager visualstates

我正在试图弄清楚VisualStateManager是如何工作的,但我不能这样做。

我想这样做:我有一些TextBoxes,用户可以输入他的名字,地址,......我正在进行验证,我不想在XAML中使用validationrules绑定。我正在使用MVVM模式,我的viewmodel的一些代码就是这样做的。所以我将文本框的文本绑定到viewmodel为我验证的字符串,我在那里有一个布尔变量,告诉它是否正确。

我的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);

因为我在这里读到它会有所帮助,但它与...相同......

我做错了什么?

1 个答案:

答案 0 :(得分:0)

我认为VisualStateManager UserControl无法使用,我不知道为什么。 我用过Control。