导出 (0) 打印
全部展开

FrameworkElement.FindName 方法

Finds an element that has the provided identifier name.

命名空间: System.Windows
程序集: PresentationFramework(在 presentationframework.dll 中)

public Object FindName (
	string name
)
public Object FindName (
	String name
)
public function FindName (
	name : String
) : Object
不能在 XAML 中使用方法。

参数

name

The name of the requested element.

返回值

The requested element. This can be 空引用(在 Visual Basic 中为 Nothing) if no matching element was found.

If the element has child elements, these child elements are all searched recursively for the requested named element.

FindName operates within the current element's namescope. For details, see WPF Namescopes.

This example describes how to use the FindName method to find an element by its Name value.

In this example, the method to find a particular element by its name is written as the event handler of a button. stackPanel is the Name of the root FrameworkElement being searched, and the example method then visually indicates the found element by casting it as TextBlock and changing one of the TextBlock visible UI properties.

void Find(object sender, RoutedEventArgs e)
{
    object wantedNode = stackPanel.FindName("dog");
    if (wantedNode is TextBlock)
    {
        // Following executed if Text element was found.
        TextBlock wantedChild = wantedNode as TextBlock;
        wantedChild.Foreground = Brushes.Blue;
    }
}

For the complete sample, see Searching for an Element Sample.

Windows Vista、Microsoft Windows XP SP2 和 Windows Server 2003 SP1 支持 Microsoft .NET Framework 3.0。

.NET Framework

受以下版本支持:3.0

社区附加资源

添加
显示:
© 2014 Microsoft