如何从后面的 C# 代码更新 WPF 绑定的值?

VIJ*_*JAY 2 c# wpf binding

使用以下代码在文本框上绑定属性。当 UI 中的文本框值被修改时,属性值将被刷新。但我将值分配给代码后面的文本框 [txtNoOfSessions.Text = "1"] ,但它没有反映属性中的值。

 <TextBox x:Name="txtNoOfSessions" 
          Text="{Binding Path=NoOfSessions,Mode=TwoWay}" 
          Height="23" Width="120" />
Run Code Online (Sandbox Code Playgroud)

Xca*_*r37 5

对属性使用 INotifyPropertyChanged。否则,UI 加载后将不会更新。

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

例子:

public string PhoneNumber
{
    get
    {
        return this.phoneNumberValue;
    }

    set
    {
        if (value != this.phoneNumberValue)
        {
            this.phoneNumberValue = value;
            NotifyPropertyChanged("PhoneNumber");
        }
    }
}
Run Code Online (Sandbox Code Playgroud)