匯出 (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 Null 參照 (即 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