繁体   English   中英

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

[英]WP7 - How to set the border color for text box 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);
    }

这段代码写在Xaml.CS中。 但是我想在ViewModel中编写所有代码。 有人让我知道如何在ViewModel中编写事件吗? 还有如何在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);
        }
    }

这也是用Xaml.Cs编写的。 如何在ViewModel中编写。 提前致谢..

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

暂无
暂无

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

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