FrameworkElement.TryFindResource(Object) Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Searches for a resource with the specified key, and returns that resource if found.
public:
System::Object ^ TryFindResource(System::Object ^ resourceKey);
public object TryFindResource (object resourceKey);
member this.TryFindResource : obj -> obj
Public Function TryFindResource (resourceKey As Object) As Object
Parameters
- resourceKey
- Object
The key identifier of the resource to be found.
Returns
The found resource, or null
if no resource with the provided key
is found.
Examples
The following example is implemented as a button handler, where the button being clicked sets its background to a resource-defined brush obtained by calling TryFindResource on itself. This walks the element tree and finds the resource (the resource itself is defined in XAML and is not shown).
void TryFind(object sender, RoutedEventArgs e) {
Button b = e.Source as Button;
b.Background = (Brush)b.TryFindResource("customBrush");
}
Private Sub TryFind(ByVal sender As Object, ByVal e As RoutedEventArgs)
Dim b As Button = TryCast(e.Source, Button)
b.Background = CType(b.TryFindResource("customBrush"), Brush)
End Sub
Remarks
If the resource is not found on the calling element, the parent resource tree is searched upward through the logical tree, in the same way that the tree would be searched if a resource were requested by key at run time. The method returns null
only if no resource of that key existed anywhere in the resource tree, per the existing conditions of the tree at the time that TryFindResource is called.
Typically you would immediately cast the return value to the type of the property that you were attempting to set with the returned resource value.
The FindResource method has similar behavior, except that it throws an exception if no resource with the provided key was returned.
Applies to
See also
.NET
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기