How how do you do this in c#?
<TextBlock Text={Binding MyProperty}/>
Assume the DataContext is set to a class of Type MyClass
Assuming your TextBlock
is called _textBlock
:
var binding = new Binding("MyProperty");
BindingOperations.SetBinding(_textBlock, TextBlock.TextProperty, binding);
You can call FrameworkElement.SetBinding() to build data binding from C#.
Simple:
<TextBlock x:Name="txt"/>
// C#
txt.SetBinding(TextBox.TextProperty, "MyProperty");
Create a Binding object and pass it to SetBinding if you want more control over the binding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With