WP7 - 如何从ViewModel设置文本框的边框颜色

时间:2014-03-03 06:19:48

标签: c# windows-phone-7 mvvm

如何使用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中编写事件? 以及如何在ViewBox中为ListBox编写选择更改事件?

 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编写的。如何在ViewModel中编写。 提前谢谢..

1 个答案:

答案 0 :(得分:0)

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

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

然后在您的ViewModel中(假设您实现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");
        }
    }
}