I can't Data Bind to a local variable in WPF/XAML

后端 未结 5 1730
无人及你
无人及你 2021-01-01 01:14

I want a textbox to display the value of a variable when I click it (an iteration of 1 to 100), I do not know what I am doing Wrong:

When I run the project nothing i

5条回答
  •  没有蜡笔的小新
    2021-01-01 01:44

    You need to make the property tell the binding that it has updated. The standard way to do this is via:

    1. Implementing INotifyPropertyChanged
    2. Making the myText property a DependencyProperty
    3. Another maybe less used way is to raise the event manually, like this:
    public void Button_Click_1(object sender, RoutedEventArgs e)
    {
        myText = "Clicked";
        BindingOperations.GetBindingExpressionBase(myTextBox, TextBlock.TextProperty).UpdateTarget();
    }
    

    Note that your TextBlock has the confusing name myTextBox

提交回复
热议问题