将 [VisualStateManager] 视图状态绑定到 MVVM 视图模型? [英] Binding [VisualStateManager] view state to a MVVM viewmodel?

查看:58
本文介绍了将 [VisualStateManager] 视图状态绑定到 MVVM 视图模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将控件的 VisualStateManager 状态绑定到视图模型中的属性?可以吗?

How do you bind the VisualStateManager state of a control to a property in you viewmodel? Can it be done?

推荐答案

其实你可以.诀窍是制作一个附加属性并添加一个实际调用GoToState的属性更改回调:

Actually you can. The trick is to make an Attached property and add a property changed callback that actually calls GoToState:

public class StateHelper {
    public static readonly DependencyProperty StateProperty = DependencyProperty.RegisterAttached( 
        "State", 
        typeof( String ), 
        typeof( StateHelper ),
        new UIPropertyMetadata( null, StateChanged ) );

      internal static void StateChanged( DependencyObject target, DependencyPropertyChangedEventArgs args ) {
      if( args.NewValue != null )
        VisualStateManager.GoToState( ( FrameworkElement )target, args.NewValue, true );
    }
  }

然后你可以在你的 xaml 中设置这个属性,并像其他任何其他一样向你的视图模型添加一个绑定:

You can then set this property in you xaml and add a binding to your viewmodel like any other:

<Window .. xmlns:local="clr-namespace:mynamespace" ..>
    <TextBox Text="{Binding Path=Name, Mode=TwoWay}"
             local:StateHelper.State="{Binding Path=State, Mode=TwoWay}" />
</Window>

NameState 是视图模型中的常规属性.当在视图模型中设置 Name 时,通过绑定或其他方式,它可以更改 State 女巫将更新视觉状态.State 也可以由任何其他因素设置,它仍然会更新文本框上的视图状态.

Name and State are regular properties in the viewmodel. When Name is set in the viewmodel, either by the binding or something else, it can change the State witch will update the visual state. State could also be set by any other factor and still it would update the view state on the textbox.

由于我们使用普通绑定绑定到状态,我们可以应用转换器或其他任何我们通常能够做的事情,因此视图模型不必知道它实际上设置了视觉状态name,State 可以是 bool 或 enum 或其他任何东西.

Since we're using a normal binding to bind to Status, we can apply converters or anything else that we'd normally be able to do, so the viewmodel doesn't have to be aware that its actually setting a visual state name, State could be a bool or an enum or whatever.

您也可以在 .net 3.5 上使用 wpftoolkit 使用这种方法,但是您必须将 target 转换为 Control 而不是 FrameworkElement>.

You can also use this approach using the wpftoolkit on .net 3.5, but you have to cast target to a Control instead of a FrameworkElement.

另一个关于视觉状态的快速说明,确保你没有命名你的视觉状态,以免它们与内置的冲突,除非你知道你在做什么.这对于验证尤其如此,因为验证引擎将尝试每次更新绑定时(以及其他时间)设置其状态.转到此处以获取有关视觉状态名称的参考不同的控件.

Another quick note on visual states, make sure you don't name your visual states so that they conflict with the built in ones unless you know what you're doing. This is especially true for validation since the validation engine will try and set its states everytime the binding is updated (and at some other times as well). Go here for a reference on visual state names for diffrent controls.

这篇关于将 [VisualStateManager] 视图状态绑定到 MVVM 视图模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆