簡體   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