开发者

WPF Dynamic resource reference

HI

Am load a string xaml with DynamicResource assigned to a Background property. Is there a way to get the reference of开发者_C百科 the dynamic resource.

Background="{DynamicResource Color1}"

I want to get the resource reference assigned to a Dependency property at runtime Pl help


Use FrameworkElement.FindResource Method

this.FindResource("Color1");


Where is the DependencyProperty defined? On the same Window/UserControl? If you simply want to bind to the value of a DependencyProperty you probably want to use regular {Binding ...} syntax instead.

Example 1: If you are binding to a dependency property on a particular control named myControl you can declare it like below.

Background="{Binding ElementName=myControl, Path=Color1}"

Example 2: If you don't want to rely on naming controls because it is so pasay in WPF and you are referencing a property defined on your Window you could do something like below.

Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=Color1}"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜