簡體   English   中英

WPF文本塊無法在運行時顯示屬性更改

[英]WPF Text Block Wont Show Property Change at Run-Time

我有一個文本塊如下

<TextBlock x:Name="lblErrorMessage" 
           Grid.Row="2" 
           Foreground="Red"   
           Margin="{Binding ControlMargin}" 
           HorizontalAlignment="Stretch" 
           VerticalAlignment="Stretch" 
           Background="Aqua"
           TextWrapping="Wrap" 
           Text="{Binding Error}"/>

還有一個物業在后面

    /// <summary>
    /// The error message
    /// </summary>
    private string _error = "kkbkbkbkbjK";

    /// <summary>
    /// The error message
    /// </summary>
    public string Error
    {
        //return the error message
        get { return _error; }
        set
        {
            //set the error message
            _error = value;

            //fire the property changed event
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Error"));
        }
    }

然后,我有一個通過更新錯誤的ICommand執行的方法

 //set the error message
 Error = "Login Succeeded";

我的問題是其他綁定也可以工作(例如下面的Margin),但是文本塊“ lblErrorMessage”絕對拒絕顯示該消息。

如果我在構造函數中設置了Error屬性,則會顯示出來。 如果我設置顯示的_error變量

看來,如果我在運行時設置該屬性,它將無法進行更改。

我已經嘗試了“雙向”,“ PropertyChanged”等設置的每種組合,但還沒有香蕉。

有任何想法嗎?

總結評論。

更改Error后,如果使用正確的屬性名稱引發PropertyChanged事件,則發布的代碼很好,但是如果您的類未實現INotifyPropertyChanged接口,則此代碼將無法工作。

public class MyViewModel: INotifyPropertyChanged

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM