(wpf) Application.Current.Resources 与 FindResource [英] (wpf) Application.Current.Resources vs FindResource

查看:55
本文介绍了(wpf) Application.Current.Resources 与 FindResource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我正在用 C# 中的 WPF 制作一个 GUI 东西.它看起来像这样:

So, I'm making a GUI thingy with WPF in C#. It looks like this:

现在还没有完成.这 2 行是我尝试制作一种数据表,它们在 XAML 中进行了硬编码.

It's not done right now. Those 2 rows are my attempt at making a sort of data table, and they are hard-coded in the XAML.

现在,我正在 C# 中实现添加新水果按钮功能.我在 XAML 中有以下样式来控制行的背景图像应该是什么样子:

Now, I am implementing the add new fruit button functionality in the C#. I have the following style in the XAML that governs what the background images of the rows should look like:

<Style x:Key="stretchImage" TargetType="{x:Type Image}">
    <Setter Property="VerticalAlignment" Value="Stretch"/>
    <Setter Property="HorizontalAlignment" Value="Stretch"/>
    <Setter Property="Stretch" Value="Fill"/>
</Style>

因此,在代码中,我为每一列创建了一个图像,col0col1col2,如果我使用以下代码,

So, in the code, I create an image for each column, col0, col1, and col2, and if I use the following code,

col0.Style = (Style)Application.Current.Resources["stretchImage"];
col1.Style = (Style)Application.Current.Resources["stretchImage"];
col2.Style = (Style)Application.Current.Resources["stretchImage"];

它添加了一个看起来像这样的新行:

it adds a new row that looks like this:

如您所见,这并不完全正确......拉伸窗口会加剧问题:

As you can see, it's not quite right... And stretching the window exacerbates the problem:

它似乎不尊重样式的Stretch"属性.

It seems to not be respecting the style's "Stretch" property.

但是,如果我改为将样式加载代码更改为

But then, if I instead change my style loading code to

col0.Style = (Style)FindResource("stretchImage");
col1.Style = (Style)FindResource("stretchImage");
col2.Style = (Style)FindResource("stretchImage");

效果很好:

(同样,应用程序还没有完成,所以不用担心),但我的主要问题是:Application.Current.Resources[] 之间有什么区别FindResource()?为什么一个似乎忽略了某些属性而另一个则没有?如果可能的话,我如何才能让 Application.Current.Resources[] 正常工作?

(Again, the app isn't finished, so don't worry about that), but my main question is: What's the difference between Application.Current.Resources[] and FindResource()? Why does one seem to ignore some of the properties while the other doesn't? And how, if at all possible, might I get Application.Current.Resources[] to work properly?

推荐答案

几乎可以在可视化树中的任何元素上定义资源.FrameworkElement.FindResource() 将沿着树向上查找每个节点处的资源,并最终使其一直到应用程序.Application.Current.Resources[] 跳过所有这些,直接访问应用程序上的资源.您几乎总是想使用 FindResource() 以便您可以在各个点覆盖样式.

Resources can be defined on almost any element in the visual tree. FrameworkElement.FindResource() will walk up the tree looking for the resource at each node, and eventually make it all the way to Application. Application.Current.Resources[] skips all of this and goes straight for the resources on the Application. You almost always want to use FindResource() so you can override styles at various points.

这篇关于(wpf) Application.Current.Resources 与 FindResource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆