当前绑定值
我正在编写标记扩展。我有像这样的 XAML
<TextBlock Text="{ui:Test SomeInfo}" />
和 TestExtension,其构造函数采用一个字符串参数。我收到“SomeInfo”字符串,所以一切都找到了。现在我想嵌套扩展并编写类似的东西
<TextBlock Text="{ui:Test {Binding PropName}}" />
,但它不能按原样工作。我必须添加采用 System.Windows.Data.Binding 类型的一个参数的构造函数。
现在我需要知道
- 如何从 Binding 对象中检索当前值?
- 我什么时候应该这样做?我应该以某种方式订阅更改还是每次在 ProvideValue 方法中询问该值?
Update1 PropName 应根据 TextBlock 的 DataContext 进行解析。
Update2刚刚找到相关问题:如何我如何解析数据绑定的值?
I'm writing markup extension. I have XAML like this
<TextBlock Text="{ui:Test SomeInfo}" />
and TestExtension with constructor taking one string argument. I'm getting "SomeInfo" string so everything is find. Now I want to nest extensions and write something like
<TextBlock Text="{ui:Test {Binding PropName}}" />
and it does not work as is. I had to add constructor which takes one argument of System.Windows.Data.Binding type.
Now I need to know
- How should I retrieve a current value from the Binding object?
- When should I do this? Should I subscribe to changes some way or ask for that value every time in ProvideValue method?
Update1 PropName should be resolved against DataContext of TextBlock.
Update2 Just found related question: How do I resolve the value of a databinding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的绑定将不起作用,因为您的
MarkupExtension
没有DataContext
并且它不会出现在可视化树中,我认为您不应该直接与绑定对象交互。您真的需要这个扩展吗?也许您可以单独使用绑定和 转换器?如果不是,您可以创建一个具有可绑定属性的专用类(通过继承
DependencyObject
),但这仍然不会给您一个DataContext
或ElementName
所需的名称范围或RelativeSource
所需的可视化树,因此在这种情况下使绑定起作用的唯一方法是使用 < code>Source (例如将其设置为静态资源
)。这并不理想。另请注意,如果您不直接设置绑定,则
ProvideValue
方法只会被调用一次,这意味着即使您的扩展中有绑定,它也可能不会非常有用(有一些例外,例如,当返回复杂内容时,例如使用绑定的ItemsControl
,但您在TextBlock.Text
上设置了扩展名,它只是一个字符串),所以我真的很怀疑如果您想使用这样的 MarkupExtension值应该根据绑定动态变化。如前所述:考虑转换器或MultiBindings 代替各种值。Bindings like this will not work because your
MarkupExtension
has noDataContext
and it does not appear in the visual tree and i do not think you are supposed to interact with binding objects directly. Do you really need this extension? Maybe you could make do with the binding alone and a converter?If not you could create a dedicated class which has bindable properties (by inheriting from
DependencyObject
), this however would still not give you aDataContext
or namescope needed forElementName
or a visual tree needed forRelativeSource
, so the only way to make a binding work in that situation is by using aSource
(e.g. set it to aStaticResource
). This is hardly ideal.Also note that if you do not directly set a binding the
ProvideValue
method will only be called once, this means that even if you have a binding in your extension it may not prove very useful (with some exceptions, e.g. when returning complex content, like e.g. anItemsControl
which uses the binding, but you set the extension onTextBlock.Text
which is just a string), so i really doubt that you want to use a MarkupExtension like this if the value should change dynamically based on the binding. As noted earlier: Consider converters or MultiBindings for various values instead.