WPF:VisualStateManager 不工作
我试图弄清楚 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 VisualStateManager 不能与 UserControl 一起使用,我不知道为什么。
我用的是控制。
I think VisualStateManager UserControl does not work with, I do not know why.
I used Control.