-1

How does one simply "Databind a textblock to a codebehind C# variable"?

In my codebehind I have a public string variable and in my XAML I have but it does not work.

Thanks in advance!

  • I am sorry, but this site is not meant for tutorials. These abound on the web by the way. But to help you get started: you must Always bind to *Properties* , not to public fields. Moreover, have some reading about the `INotifyPropertyChanged` interface. – Dabblernl Jun 14 '13 at 23:05

1 Answers1

2

Use the BindingOperations class. Something like this.

BindingOperations.SetBinding(myTextBlock, TextBlock.TextProperty, new Binding("Whatever"))

Without knowing more about what you're trying to bind, this is the best answer you'll get.

And it's almost always better to use a real MVVM pattern rather than to do this sort of thing in codebehind.

Michael Gunter
  • 12,528
  • 1
  • 24
  • 58