WP7 - 如何从视图模型设置文本框的边框颜色



如何使用ViewModel中的GotFocus()和LostFocus()?

 private void TxtDescribeGroup_GotFocus(object sender, RoutedEventArgs e)
    {
        TxtDescribeGroup.BorderBrush = new SolidColorBrush(Colors.Orange);
    }
    private void TxtDescribeGroup_LostFocus(object sender, RoutedEventArgs e)
    {
        TxtDescribeGroup.BorderBrush = new SolidColorBrush(Colors.Gray);
    }

此代码编写在 Xaml.CS 中。但我想在 ViewModel 中编写所有代码。有人告诉我如何在ViewModel中编写事件吗?以及如何在列表框的视图模型中编写选择更改事件?

 private void lstShow_Tap(object sender, GestureEventArgs e)
    {
        if (lstShow.SelectedItem != null)
        {
            ListBox item = (ListBox)sender;
            LoginPageModel listItem = (LoginPageModel)item.SelectedItem;
            MessageBox.Show("Selected FirstName==> " + listItem.FirstName);
        }
    }

这也是用Xaml.Cs编写的。如何在视图模型中编写。提前谢谢..

在 XAML 中(假设你已经设置了DataContext

<Border BorderBrush="{Binding Path=BorderBrush}">
    ... your stuff here
</Border>

然后在您的视图模型中(假设您实现了INotifyPropertyChanged),只需添加一个属性:

private Brush borderBrush;
public Brush BorderBrush {
    get { return borderBrush; }
    set {
        if(value!=borderBrush) {
            value=borderBrush;
            // this notifies your UI that the property has changed and it should read the new value
            // should be already declared in your view model or base view model or whatever
            // MVVM framework you are using
            OnPropertyChanged("BorderBrush");
        }
    }
}

最新更新