I cannot bind data to a local variable in WPF / XAML

I want the text box to display the value of the variable when I click on it (iterate from 1 to 100), I don't know what I'm doing Wrong:

When I start the project, nothing is displayed in the text box.

What is the best way to display variables in a text box?

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace dataBindingTest { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public string myText { get; set; } public void Button_Click_1(object sender, RoutedEventArgs e) { int i = 0; for (i = 0; i < 100; i++) { myText = i.ToString(); } } } } 

XAML:

 <Window x:Class="dataBindingTest.MainWindow" Name="windowElement" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <Button Content="Button" HorizontalAlignment="Left" Height="106" Margin="71,95,0,0" VerticalAlignment="Top" Width="125" Click="Button_Click_1"/> <TextBlock x:Name="myTextBox" HorizontalAlignment="Left" Height="106" Margin="270,95,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="187" Text= "{Binding myText, ElementName=windowElement}" /> </Grid> </Window> 
+6
source share
5 answers

Your current property myText cannot notify the WPF binding system when its value has changed, so the TextBlock will not be updated.

If you make this a dependency property, instead, it automatically implements a change notification, and the property changes will be reflected in the TextBlock .

So, if you replace the public string myText { get; set; } public string myText { get; set; } public string myText { get; set; } for all this code, it should work:

 public string myText { get { return (string)GetValue(myTextProperty); } set { SetValue(myTextProperty, value); } } // Using a DependencyProperty as the backing store for myText. This enables animation, styling, binding, etc... public static readonly DependencyProperty myTextProperty = DependencyProperty.Register("myText", typeof(string), typeof(Window1), new PropertyMetadata(null)); 
+10
source

implement INotifyPropertyChanged :

 public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { this.InitializeComponent(); } private string _txt; public string txt { get { return _txt; } set { if (_txt != value) { _txt = value; OnPropertyChanged("txt"); } } } private void Button_Click(object sender, RoutedEventArgs e) { txt = "changed text"; } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } } 

XAML:

 <TextBox Text="{Binding txt}"/> <Button Click="Button_Click">yes</Button> 

and don't forget to add the DataContext property of your window:

 <Window ... DataContext="{Binding RelativeSource={RelativeSource Self}}"/> 
+7
source

Try the following:

  public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); this.DataContext = this; } public string myText { get; set; } public void Button_Click_1(object sender, RoutedEventArgs e) { BackgroundWorker bw = new BackgroundWorker(); bw.DoWork += delegate { int i = 0; for (i = 0; i < 100; i++) { System.Windows.Threading.Dispatcher.CurrentDispatcher.Invoke((Action)(() => { myText = i.ToString(); OnPropertyChanged("myText"); })); Thread.Sleep(100); } }; bw.RunWorkerAsync(); } public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string name) { PropertyChangedEventHandler handler = PropertyChanged; if (handler != null) { handler(this, new PropertyChangedEventArgs(name)); } } } 

XAML file:

  <Grid> <Button Content="Button" HorizontalAlignment="Left" Height="106" Margin="71,95,0,0" VerticalAlignment="Top" Width="125" Click="Button_Click_1"/> <TextBlock x:Name="myTextBox" HorizontalAlignment="Right" Height="106" Margin="0,95,46,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="187" Text= "{Binding myText}" /> </Grid> 
+3
source

You must implement INotifyPropertyChanged in your "MainWindow" so that your "myTextBlock" can automatically receive changes from your data and update.

So your "MainWindow" should look like this:

 public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); } private string _myText; public string myText { get{return _myText;} set{_myText = value; if(PropertyChanged!=null) PropertyChanged(this, new PropertyChangedEventArgs("myText")) ; } } public event PropertyChangedEventHandler PropertyChanged; etc..... } 
+1
source

You need to force the property to specify the binding that it updated. The standard way to do this is through:

 public void Button_Click_1(object sender, RoutedEventArgs e) { myText = "Clicked"; BindingOperations.GetBindingExpressionBase(myTextBox, TextBlock.TextProperty).UpdateTarget(); } 

Note that your TextBlock has a confusing name myTextBox

0
source

All Articles