WP7 绑定到局部变量 [英] WP7 Binding to Local Variable
问题描述
我有一个 UI 元素,我想根据页面中的局部变量设置其颜色(实际上在模块中,但我可以将其移动到页面).
I have a UI element whose color I'd like to set based on a local variable in the page (well actually in a module, but I can move it to the page).
我在 WPF 中看到,可以通过将变量设为属性并使用 FindAncestor 将 RelativeSource 绑定到父类来将 UI 元素绑定到局部变量:
I see in WPF one can bind a UI element to a local variable by making the variable a property and setting the binding RelativeSource to the parent class using FindAncestor:
遗憾的是 Silverlight 似乎不支持 FindAncestor 模式.
Unfortunately Silverlight doesn't seem to support FindAncestor mode.
任何可能的解决方案,或者我是否必须为这个变量创建一个自定义类的实例?
Any possible solution, or do I have to create an instance of a custom class and such just for this one variable?
推荐答案
在过去的几周里,我从 Silverlight 跳到 WPF 很多,所以下面的想法可能会也可能不会.
Ive been jumping from Silverlight to WPF a lot over the last few weeks, so the following idea may or may not fly.
您的页面是否设置了 name 属性?如果没有,设置它 (x:Name="MyPage").
Does your page have the name property set on it? If not, set it (x:Name="MyPage").
那么你可以这样做:
Fill="{Binding ElementName=MyPage, Path=MyProperty}"
试试看(就在我的头顶)
Give that a try (just off the top of my head)
这篇关于WP7 绑定到局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!