Silverlight MVVM visualstatemanager和focus
使用Silverlight 4 我的控件有两种可视状态。我想在状态改变时将焦点从一个文本框更改为另一个文本框 使用MVVM实现这一点的最佳方法是什么Silverlight MVVM visualstatemanager和focus,silverlight,silverlight-4.0,visualstatemanager,Silverlight,Silverlight 4.0,Visualstatemanager,使用Silverlight 4 我的控件有两种可视状态。我想在状态改变时将焦点从一个文本框更改为另一个文本框 使用MVVM实现这一点的最佳方法是什么 我希望用visualstatemanager来做这件事或者做一个行为。。。但是我还没有想出一个办法。在文本框之间更改焦点是视图特定的代码,所以我认为应该在视图后面的代码中完成。有些人建议根本没有代码,但我认为这有点夸张 至于如何从ViewModel触发它,我将执行以下操作: class MyView : UserControl { //
我希望用visualstatemanager来做这件事或者做一个行为。。。但是我还没有想出一个办法。在文本框之间更改焦点是视图特定的代码,所以我认为应该在视图后面的代码中完成。有些人建议根本没有代码,但我认为这有点夸张 至于如何从ViewModel触发它,我将执行以下操作:
class MyView : UserControl {
// gets or sets the viewmodel attached to the view
public MyViewModel ViewModel {
get {...}
set {
// ... whatever method you're using for attaching the
// viewmodel to a view
myViewModel = value;
myViewModel.PropertyChanged += ViewModel_PropertyChanged;
}
private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName == "State") {
VisualStateManager.GoToState(this, ViewModel.State, true);
if (ViewModel.State == "FirstState") {
textBox1.Focus();
}
else if (ViewModel.State == "SecondState") {
textBox2.Focus();
}
}
}
}
class MyViewModel : INotifyPropertyChanged {
// gets the current state of the viewmodel
public string State {
get { ... }
private set { ... } // with PropertyChanged event
}
// replace this method with whatever triggers your
// state change, such as a command handler
public void ToggleState() {
if (State == "SecondState") { State = "FirstState"; }
else { State = "SecondState"; }
}
}
在文本框之间更改焦点是视图特定的代码,所以我认为应该在视图的代码后面完成。有些人建议根本没有代码,但我认为这有点夸张 至于如何从ViewModel触发它,我将执行以下操作:
class MyView : UserControl {
// gets or sets the viewmodel attached to the view
public MyViewModel ViewModel {
get {...}
set {
// ... whatever method you're using for attaching the
// viewmodel to a view
myViewModel = value;
myViewModel.PropertyChanged += ViewModel_PropertyChanged;
}
private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) {
if (e.PropertyName == "State") {
VisualStateManager.GoToState(this, ViewModel.State, true);
if (ViewModel.State == "FirstState") {
textBox1.Focus();
}
else if (ViewModel.State == "SecondState") {
textBox2.Focus();
}
}
}
}
class MyViewModel : INotifyPropertyChanged {
// gets the current state of the viewmodel
public string State {
get { ... }
private set { ... } // with PropertyChanged event
}
// replace this method with whatever triggers your
// state change, such as a command handler
public void ToggleState() {
if (State == "SecondState") { State = "FirstState"; }
else { State = "SecondState"; }
}
}
如果我是你,我会创建一个FocusBehavior,使用FocusBehavior.IsFocused属性,将该行为添加到控件上,并在VSM状态集IsFocused=True中 如果我是你,我会创建一个FocusBehavior,使用FocusBehavior.IsFocused属性,将该行为添加到控件上,并在VSM状态集IsFocused=True中 与JustinAngle的答案非常相似,但我认为这是Silverlight特有的解决方案,值得一提。基本上,Jeremy Likess创建了一个虚拟控件,他称之为FocusHelper,其行为非常类似于FocusBehavior。这与JustinAngle的答案非常相似,但我认为这是一个Silverlight特有的解决方案,值得一提。基本上Jeremy Likess创建了一个虚拟控件,他称之为FocusHelper,其行为非常类似于FocusBehavior