简体   繁体   中英

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

How to use GotFocus() and LostFocus() from ViewModel?

 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. But I want to write all the code in ViewModel. Any one let me know how to write the events in ViewModel? And also how to write the selection changed event in ViewModel for 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. How to write in the ViewModel. Thanks in advance..

In XAML (assuming you already set your DataContext )

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

Then in your ViewModel (assuming you implement INotifyPropertyChanged ) just add a property:

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");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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