在 WPF 中获取动态资源绑定

发布于 2024-08-11 16:35:41 字数 474 浏览 2 评论 0原文

任何人都可以帮助我通过代码在 WPF 中获取 DynamicResource Binding 吗?

我已经在 Xaml 中设置了绑定,如下所示,

TextBlock Background={DynamicResource ColorA} Name="TB"。

我需要得到 - TB 的背景绑定到“ColorA”。 我如何通过编码访问 WPF 中的动态资源绑定表达式。 当我尝试获取 TB.Background 时,它是白色的(#FFFFF..),或者如果我已经将值赋予 将给出的资源键“ColorA”。 但我想得到这个绑定表达式。 预先感谢您的帮助。


我认为我的问题不清楚。 我想通过代码获取 Xaml 中“TB”的资源绑定完成情况。 但没有任何 TB.GetResourceReference。我想要一些这样的想法。 该绑定表达式保存在 WPF 中的位置。我需要获取 TB 的背景属性是 绑定到哪个(答案“ColorA”)键? 非常感谢突然的回复。

Can any one help me to get DynamicResource Binding in WPF by code?

I have set binding Like follow,

TextBlock Background={DynamicResource ColorA} Name="TB" in Xaml.

and i need to get the - TB's background is binded to "ColorA".
how can i access this DynamicResource Binding Expression in WPF by coding.
when i try to get TB.Background, it is white(#FFFFF..) or if i already given the value to the
Resorce key "ColorA" that will be given.
but i want to get this Binding Expression.
Thank in advance for your Help.


I think my Question wasn't clear.
I want to get What Reource Binding was done to the "TB" in Xaml by code.
But the aren't any TB.GetResourceReference. I Want some think like that.
Where that Binding expression is kept in WPF. I need to get the TB's BackgroundProperty was
Binded to Which( answer "ColorA") key?
thank a lot for sudden response.

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

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

发布评论

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

评论(3

坐在坟头思考人生 2024-08-18 16:35:41

您可以使用 FrameworkElement.SetResourceReference 方法:

MSDN: http ://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.setresourcereference.aspx

如果你的xaml有这个:

<TextBlock x:Name="TB">

你可以在后面的代码中写这个:

TB.SetResourceReference(BackgroundProperty, "ColorA");

You can use the FrameworkElement.SetResourceReference method:

MSDN: http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.setresourcereference.aspx

Provided your xaml has this:

<TextBlock x:Name="TB">

You can write this in the code behind:

TB.SetResourceReference(BackgroundProperty, "ColorA");
尾戒 2024-08-18 16:35:41

你可以使用这个:

YourControl.Style = this.FindResource(NameOfYourStyleForThisControl) as Style;

You can use this:

YourControl.Style = this.FindResource(NameOfYourStyleForThisControl) as Style;
辞旧 2024-08-18 16:35:41

您可以通过执行以下操作来覆盖其父级内部的 ColorA

<TextBlock.Resources>
    <Background x:Key="ColorA">your color</Background >
<TextBlock.Resources>

You can override ColorA inside it's parent by doing this

<TextBlock.Resources>
    <Background x:Key="ColorA">your color</Background >
<TextBlock.Resources>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文