WPF如何获得对资源元素的引用? [英] How to get reference to element in resources, WPF?
问题描述
我对WPF有疑问-我是这项智能技术的新手。所以问题是:
I have problem with WPF - I'm quiet new in this smart technology. So the problem is:
我有一个窗口。在此窗口的资源中,我存储了一个元素-例如一个具有唯一键的网格(假设x:Key = myGrid)。在此网格中,我有一个由名称(x:Name = myTextBox)标识的TextBox。
我的窗口仅包含一个空网格(例如,winGrid)。我以编程方式将myGrid设置为winGrid的子级。现在,在运行时中,我想获得对myTextBox对象的引用。我花了大量的时间在谷歌上搜索,但对我没有任何帮助(FindName和类似的方法)。
I have a window. In this window's resources I have stored an element - eg. a Grid with unique key (assume x:Key="myGrid"). In this Grid I have a TextBox identified by a name (x:Name="myTextBox"). My Window contains only an empty Grid (named eg. winGrid). I programmatically set the myGrid as a child of the winGrid. And now, in runtime, I want to get a reference to the myTextBox object. I spent plenty of time googling, but nothing worked for me (FindName and similar methods).
您有任何想法吗,我需要做些什么才能拿到球
Do you have please any idea, what I have to do to get the ball rolling?
这里是(伪)代码片段了:
Here is (pseudo)code snippet once more:
<Window x:Class="LoginForm.RidicWindow"
...>
<Window.Resources>
<Grid x:Key="myGrid">
<Border...
<Grid...
...
<TextBlock x:Name="myTextBlock" Grid.Column="0".../>
</Grid>
</Grid>
</Window.Resources>
<Grid x:Name="winGrid">
...
</Grid>
现在我将myGrid设置为winGrid的子级:
(类似)
And now I set the myGrid as a child of winGrid: (something like)
winGrid.Childrens.Clear();
winGrid.Childrens.Add((Grid)FindResource(myGrid));
现在我想获得对myTextBlock的引用,它是myGrid的后代。
And now I want to get a reference to myTextBlock, which is descendant of the myGrid.
我尝试了类似的
((Grid)FindResource(myGrid)).FindByName("myTextBlock");
这当然是行不通的。
希望您了解我,我想要得到什么。
非常感谢!
Hope you understand me, what I want to get. Lot of thanks!
推荐答案
您不能这样做(顺便说一句,可以,但这确实很糟糕,丑陋,不建议使用)窗口的资源可用于其他目的。
You can not do this (By the way, you can, but it's really bad, ugly and not recommended) The resources of a window to serve another purpose.
如上所述,您必须创建一个组件(Usercontrol或其他组件)。您寻求的其他选择。
您可以尝试以下我写的内容:
As mentioned, you must create a component (Usercontrol or other).. Although there are some other options for what you seek. You can try some of what I wrote below:
1)创建自定义组件可以是UserControl,Grid或其他任何东西...
1) Creating a custom component may be a UserControl, Grid or anything else...
<Grid x:Class="Project.MyGridControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<!-- Content -->
</Grid>
和
MyGridControl control = new MyGridControl();
winGrid.Childrens.Add(control);
2)稍微复杂一点:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<!-- Content -->
</Grid>
和
Grid myCustomGrid = XamlReader.Load(uriResource) as Grid;
winGrid.Childrens.Add(myCustomGrid);
在此选项中,您将不需要像网格控件那样实例化。 (我看到它经常在报告中使用)。您应该创建一个.xaml并将其定义为资源。
In this option you will not have like a grid control to instantiate. (I see it often used in reports). You should create a .xaml and define it as a resource.
要查找组件,您应该在可视树中查看(如已回复)...
To find components you should look in the visual tree (as already responded)...
这篇关于WPF如何获得对资源元素的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!