C# Silverlight 中的编程绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/71932/
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
Programmatic binding in Silverlight
提问by MojoFilter
I'm missing the boat on something here, kids. This keeps rearing its head and I don't know what's going on with it, so I hope my homeys here can help.
孩子们,我错过了这里的船。这一直在抬头,我不知道这是怎么回事,所以我希望我在这里的家人可以提供帮助。
When working in Silverlight, when I create bindings in my c# code, they never hold up when the application is running. The declarative bindings from my xaml seem ok, but I'm doing something wrong when I create my bindings in C#. I'm hoping that there is something blindingly obvious I'm missing. Here's a typical binding that gets crushed:
在 Silverlight 中工作时,当我在我的 c# 代码中创建绑定时,它们在应用程序运行时永远不会停止。我的 xaml 中的声明性绑定似乎没问题,但是当我在 C# 中创建我的绑定时我做错了。我希望我遗漏了一些显而易见的东西。这是一个典型的绑定被粉碎:
TextBlock tb = new TextBlock();
Binding b = new Binding("FontSize");
b.Source = this;
tb.SetBinding(TextBlock.FontSizeProperty, b);
采纳答案by MojoFilter
It looks like as of Silverlight 3.1, at least, this is no longer an issue. I can't reproduce it, at any rate.
它看起来像 Silverlight 3.1,至少,这不再是一个问题。无论如何,我无法重现它。
回答by MojoFilter
I've just tried the exact code you just posted and it worked fine, with some changes. I believe the problem is the element you are using for the SetBinding call is not the textblock you want to bind. It should be:
我刚刚尝试了您刚刚发布的确切代码,它运行良好,但进行了一些更改。我相信问题在于您用于 SetBinding 调用的元素不是您要绑定的文本块。它应该是:
TextBlock tb = new TextBlock();
Binding b = new Binding("FontSize");
b.Source = this;
tb.SetBinding(TextBlock.FontSizeProperty, b);
Make sure you also have a FontSize public property of type double on "this". If "this" is a user control, I would recommend renaming the property so you don't hide the inherited member.
确保在“this”上还有一个 double 类型的 FontSize 公共属性。如果“this”是用户控件,我建议重命名该属性,这样您就不会隐藏继承的成员。