开发者

Current binding value

开发者 https://www.devze.com 2023-04-08 00:28 出处:网络
I\'m writing markup extension. I have XAML like this <TextBlock Text=\"{ui:Test SomeInfo}\" /> and Tes开发者_如何学PythontExtension with constructor taking one string argument. I\'m getting \

I'm writing markup extension. I have XAML like this

<TextBlock Text="{ui:Test SomeInfo}" />

and Tes开发者_如何学PythontExtension 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

  1. How should I retrieve a current value from the Binding object?
  2. 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?


Bindings like this will not work because your MarkupExtension has no DataContext 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 a DataContext or namescope needed for ElementName or a visual tree needed for RelativeSource, so the only way to make a binding work in that situation is by using a Source (e.g. set it to a StaticResource). 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. an ItemsControl which uses the binding, but you set the extension on TextBlock.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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号