WPF 动态资源引用

发布于 2024-08-10 13:21:11 字数 173 浏览 2 评论 0原文

您好,

我加载一个字符串 xaml,并将 DynamicResource 分配给背景属性。有没有办法获取动态资源的引用。

Background="{DynamicResource Color1}"

我想在运行时获取分配给依赖项属性的资源引用 请帮忙

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葬心 2024-08-17 13:21:11

DependencyProperty 在哪里定义的?在同一个窗口/用户控件上?如果您只想绑定到 DependencyProperty 的值,您可能需要使用常规的 {Binding ...} 语法。

示例 1:如果您要绑定到名为 myControl 的特定控件上的依赖属性,您可以像下面这样声明它。

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

示例 2:如果您不想依赖命名控件,因为它在 WPF 中非常简单,并且您正在引用 Window 上定义的属性,您可以执行如下操作。

Background="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=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}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文