TextBlock Text属性仅绑定一次绑定模式设置为TwoWay

时间:2018-05-01 04:31:06

标签: c# wpf binding

源代码:

class Cons
{
    public Cons()
    {
        BaseDir = AppDomain.CurrentDomain.BaseDirectory;
        SetProperty();
    }
    private void SetProperty()
    {
        NowPlaying = "Hello";
    }
    public string NowPlaying{get; set; }
}
public partial class MainWindow
{
    Cons Resources = new Cons()
    public MainWindow()
    {
         txbl.DataContext = Resources;
         Resources.NowPlaying = "NoHello";
    }
}

一个文本块<Textblock x:Name="txbl" Text="{Binding NowPlaying, Mode=TwoWay}"/> 当我在MainWindow构造函数中设置NowPlaying属性时,属性更改为“NoHello”,但TextBlock文本属性仍然是Hello,即使我设置了绑定模式= TwoWay

问题出现时我捕获的图像 enter image description here

1 个答案:

答案 0 :(得分:1)

您提到的代码工作正常。文本块确实显示新值“NoHello”。 但是,如果您希望在加载文本块后更新属性值NowPlaying,请在类Cons上实现INotifyPropertyChanged,并在属性NowPlaying的setter中引发PropertyChanged。