如何通过视图模型改变的VisualState [英] How to change VisualState via ViewModel

查看:225
本文介绍了如何通过视图模型改变的VisualState的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这样的问题是类似于许多。无论如何,我不明白。

I know this Question is similar to many. Anyway, I don't understand.

我有几个 VisualStates (大于2,这就是为什么 DataStateBehavior 是不是我的解决方案)。我有视图模型,其中有枚举属性的 currentState的。每一个枚举值代表一个国家,也可能是几个枚举值代表一个国家,不梅特。我想的VisualState 改变时,在 currentState的更改(思想,即会立即出现在我的脑海:!是正是这种情况下创建绑定

I have a several VisualStates (more than 2, thats why DataStateBehavior is not my solution). And I have ViewModel, which have enum property CurrentState. Every enum value represents to one state, also may be several enum values represents to one states, doesn't metter. I want VisualState changed when the CurrentState changed (thought, that immediately appears in my mind : Binding was created exactly for this case!)

我可以绑定的 currentState的与视图的的VisualState (仅XAML的解决方案),以获得上述的行为?

Can I bind CurrentState with view VisualState (xaml-only solution), to get the behavior described above?

如果有,我该怎么办呢?

If yes, how can I do it?

如果没有,我应该如何使用 VisualStateManager.GoToState ()在我的视图模型

If no, how should I use VisualStateManager.GoToState() method in my ViewModel?

推荐答案

我不会使用VisualStateManager。 GotoState函数在视图模型的一些原因,你必须在其可视状态你要改变控制传递的最大福祉。 。传递到您的视图模型违背了整个MVVM方法UI控件了。

I wouldn't use VisualStateManager.GoToState in the ViewModel for a number of reasons, the biggest being you have to pass in the control whose visual state you're going to change. Passing UI controls up to your viewmodel goes against the whole MVVM approach.

我的建议是使用(对于Windows 8商店)的 WinRT的行为研究或使用混合system.windows.interactivity.dll(对于相同的功能)从视图模型采取的VisualState名和更新的对象。该守则将是这个样子:

My suggestion is to use (for Windows 8 Store) Winrt Behaviours or to use the Blend system.windows.interactivity.dll (for same functionality) to take a VisualState name from a viewmodel and update an object. The code would look something like this:

视图模型:

public string State{
    get{_stateName;}
    set{_stateName=value;
        RaisePropertyChanged("State");
}

查看:

<Grid>
    <I:Interaction.Behaviors>
        <b:VisualStateSettingBehavior StateToSet="{Binding State}"/>
    </i:Interaction.Behaviors>
</Grid>



行为:

Behavior:

public class VisualStateSettingBehavior:Behavior<Control>
{

    StateToSet{
               get{GetValue(StateProperty) as string;}
               set{SetValue{StateProperty,value);
                    LoadState();}
}
private void LoadState()
{
VisualStateManager.GoToState(AssociatedObject,StateToSet,true);
}
}



什么行为正在做的是,连接到控制并允许你以编程的方式,来扩展其功能。这种方法可以让你保持视图模型从视图中分离出来。

What the behaviour is doing is connecting up to the control and allowing you, in a programmatic way, to extend its functionality. This approach allows you to keep the ViewModel separate from your View.

这篇关于如何通过视图模型改变的VisualState的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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