简体   繁体   中英

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 the dynamic resource.

Background="{DynamicResource Color1}"

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

使用FrameworkElement.FindResource方法

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}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM