程序化数据绑定

Jos*_*ose 4 c# wpf xaml mvvm

你如何在c#中做到这一点?

 <TextBlock Text={Binding MyProperty}/>
Run Code Online (Sandbox Code Playgroud)

假设DataContext设置为Type MyClass类

Ken*_*art 6

假设你TextBlock的名字叫_textBlock:

var binding = new Binding("MyProperty");
BindingOperations.SetBinding(_textBlock, TextBlock.TextProperty, binding);
Run Code Online (Sandbox Code Playgroud)


And*_*ndy 2

您可以调用FrameworkElement.SetBinding()从 C# 构建数据绑定。