【问题标题】:WP7 - How to set the border color for text box from ViewModelWP7 - 如何从 ViewModel 设置文本框的边框颜色
【发布时间】:2014-03-03 17:10:04
【问题描述】:

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

【问题讨论】:

    标签: c# windows-phone-7 mvvm


    【解决方案1】:

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

    【讨论】:

    • 谢谢你的例子。这对我很有用。再次感谢.. (:
    猜你喜欢
    • 1970-01-01
    • 2016-03-14
    • 1970-01-01
    • 2014-12-14
    • 2014-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    相关资源
    最近更新 更多