C# (wpf) Application.Current.Resources 与 FindResource
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17704969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
(wpf) Application.Current.Resources vs FindResource
提问by Pojo
So, I'm making a GUI thingy with WPF in C#. It looks like this:
所以,我正在用 C# 中的 WPF 制作一个 GUI 东西。它看起来像这样:
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.
现在还没有完成。这两行是我尝试制作一种数据表,它们在 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:
现在,我正在 C# 中实现添加新水果按钮功能。我在 XAML 中有以下样式来控制行的背景图像应该是什么样子:
<Style x:Key="stretchImage" TargetType="{x:Type Image}">
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="Stretch" Value="Fill"/>
</Style>
So, in the code, I create an image for each column, col0
, col1
, and col2
, and if I use the following code,
因此,在代码中,我为每一列col0
、col1
、 和col2
,创建了一个图像,如果我使用以下代码,
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:
正如你所看到的,这不太对劲……拉伸窗口加剧了这个问题:
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");
It works beautifully:
它工作得很好:
(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?
(同样,应用程序还没有完成,所以不用担心),但我的主要问题是:Application.Current.Resources[]
和之间有什么区别FindResource()
?为什么一个似乎忽略了某些属性而另一个则没有?如果可能的话,我如何Application.Current.Resources[]
才能正常工作?
采纳答案by philq
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.
几乎可以在可视化树中的任何元素上定义资源。FrameworkElement.FindResource() 将沿着树向上查找每个节点处的资源,并最终使其一直到应用程序。Application.Current.Resources[] 跳过所有这些,直接访问应用程序上的资源。您几乎总是想使用 FindResource() 以便您可以在各个点覆盖样式。