wpf 代码隐藏文本块中的绑定字符串属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10131637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:22:36  来源:igfitidea点击:

Binding String Property in Code-Behind TextBlock

wpfbindingcode-behind

提问by Diego Vieira

i am trying to binding a very simple property to a TextBlock, but I have to do all in code-behind (C#).

我正在尝试将一个非常简单的属性绑定到 TextBlock,但我必须在代码隐藏 (C#) 中完成所有操作。

What i am trying to do is:

我想做的是:

public string SomeText { get; set; }

And after I try the Binding on TextBlock:

在我尝试对 TextBlock 进行绑定之后:

Binding myBinding = new Binding(SomeText);
myTextBlock.SetBinding(TextBlock.TextProperty, myBinding);

How do I keep the Text property of the TextBlock the same of the Property SomeText.

如何保持 TextBlock 的 Text 属性与 Property 相同SomeText

回答by Phil

Use BindingOperations

使用绑定操作

Binding binding = new Binding();
binding.Path = new PropertyPath("SomeText");
binding.Source = sourceObject;  // view model?

BindingOperations.SetBinding(theTextBlock, TextBlock.TextProperty, binding);