I want to dynamically change TextBlock text in my Class.
XAML-Code:
<TextBlock Name="Footer_text" Text=""/>
C#:
string footerMainMenuText = "Setting";
Binding set = new Binding("");
set.Mode = BindingMode.OneWay;
set.Source = footerMainMenuText;
Footer_text.DataContext = footerMainMenuText;
Footer_text.SetBinding(TextBlock.TextProperty, set);
I checked last line, and the Footer_text.Text is set correctly. ( Footer_text.Text="Setting"), but TextBlock in my application doesnt show “Setting”. What is the problem here?
If you are binding – why not just do it in XAML instead? Looking at your code it’s kind of pointless – you might as well just go
You should ideally do it in XAML or at least provide something for it to bind to
I’m not sure why you would bind a ‘string’ on it’s own to anything…do you have an object which you need to bind to the text property?
Also using
What does that do? A blank path? Not sure what the binding target would be there… have you tried
instead?
Edit:
Also the reason why your binding is not updating the control, is probably because you haven’t bound to an object which implements INotifyPropertyChanged or a similar interface. The controls need to know when values have changed, so I’d imagine that binding to ‘string’ isn’t giving the TextBlock the proper notification when it changes
Edit 2:
Here is a quick example of binding working:
My window class Window.cs:
The code behind in Window.xaml.cs
The object to bind to (with INotifyPropertyChanged)
Clicking the buttons changes SomeObject.Name, but it updates the textbox.