代码隐藏文本块中的绑定字符串属性 [英] Binding String Property in Code-Behind TextBlock

查看:23
本文介绍了代码隐藏文本块中的绑定字符串属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我想做的是:

public string SomeText { get; set; }

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

And after I try the Binding on TextBlock:

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

如何使 TextBlock 的 Text 属性与 SomeText 属性相同.

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

推荐答案

使用 BindingOperations

Use BindingOperations

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

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

这篇关于代码隐藏文本块中的绑定字符串属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆