开发者

WPF Dynamic resource reference

开发者 https://www.devze.com 2022-12-11 01:41 出处:网络
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.

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

精彩评论

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

关注公众号