开发者

Programmatically setting WPF property

I have a TextBlock control and I am declaratively setting t开发者_运维百科he Text property to {loc:Translate}.

<TextBlock x:Name="Message" Text="{loc:Translate}" loc:TranslateExtension.Uid="myMessage"  />

During runtime, I update the Text property value to something else. I then need to reset it back to {loc:Translate}.

My question is, how can I programmatically set Text to {loc:Translate}?


When a XAML processor processes a type node and member value that is a markup extension, it invokes the ProvideValue method of that markup extension and writes the result into the object graph or serialization stream. The XAML object writer passes service context to each such implementation through the serviceProvider parameter.

tb.Text = new TranslateExtension().ProvideValue(null) as string;

This should be it except that this is lacking a 'proper' IServiceProvider-parameter.

In typical usage, the .NET Framework XAML Services and the implemented XAML object writers will provide a service provider to all value converter methods it invokes during XAML processing. However, for robustness, you should provide code paths for null values both for the service provider itself and for any requested service. Null values might occur if your markup extension is applied in some circumstance where the typical service support provided by a XAML parser infrastructure is not available.

So depending on your extension implementation you could actually pass null to ProvideValue.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜