简体   繁体   English

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

[英]WP7 - How to set the border color for text box from ViewModel

How to use GotFocus() and LostFocus() from ViewModel? 如何从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);
    }

This code written in the Xaml.CS. 这段代码写在Xaml.CS中。 But I want to write all the code in ViewModel. 但是我想在ViewModel中编写所有代码。 Any one let me know how to write the events in ViewModel? 有人让我知道如何在ViewModel中编写事件吗? And also how to write the selection changed event in ViewModel for ListBox? 还有如何在ViewModel中为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);
        }
    }

This is also written in Xaml.Cs. 这也是用Xaml.Cs编写的。 How to write in the ViewModel. 如何在ViewModel中编写。 Thanks in advance.. 提前致谢..

In XAML (assuming you already set your DataContext ) 在XAML中(假设您已经设置了DataContext

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

Then in your ViewModel (assuming you implement INotifyPropertyChanged ) just add a property: 然后在您的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");
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM