Files
docs-desktop/dotnet-desktop-guide/net/wpf/systems/xaml-resources-and-code.md
T
Andy (Steve) De GeorgeandGenevieve Warren ea8529f082 WPF publish initial content (#1032)
* Add overview article for new WPF (#178)

* Remove previous WPF .NET 5 content

* Overview article

* New article: Create new WPF project (#183)

* Basic index file

* Finish article for new project.

* acro

* markdown fix

* Fix build errors

* fix code lang

* fix code lang

* Add differences article for WPF (#186)

* Fix VS version for create app

* Add differences article

* Minor

* fix overview styles code

* Add code langs for overview

* Port Window Overview WPF article (#1011)

* Add Window overview article

* Add TOC

* Remove temp code

* Fix headers

* Port final Windows related articles (#1015)

* Initial test commit

* Add system dialogs

* 75% complete

* Add images

* Fix linter

* Add to toc

* Add more howto

* Fix code

* Add get/set main window

* Add missing code

* Add to toc

* Fix warnings

* Fix warnings

* missing code

* Update see also

* fix xref

* Migrate wpf net core controls-styles articles (#1023)

* Migrate control-styles

* Fix links

* Fix links

* Port databinding article. (#1022)

* Migrated databinding overview

* update toc

* Fix links/snippets

* fix links

* Move to correct folder

* Port initial resources docs (#1026)

* initial resources docs

* Build errors

* Update dotnet-desktop-guide/net/wpf/systems/xaml-resources-overview.md

Co-authored-by: Genevieve Warren <[email protected]>

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <[email protected]>

* Feedback

* Add app article

* Add system article

* Minor

* meta

* fix toc

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <[email protected]>

Co-authored-by: Genevieve Warren <[email protected]>

* Add xaml article

* reformat redirect

* Redirects

* WPF update XAML article  (#1031)

* convert snippets

* minor edits

* minor

* Fix warnings

* Fixes #1028

* Fixes #1028

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <[email protected]>

* update redirects

Co-authored-by: Genevieve Warren <[email protected]>

* minor updates to link

* Readd migration article

* Warning fixes

* Fix lint

* Minor updates

* Fix warnings for TOC/YML

* last warnings

Co-authored-by: Genevieve Warren <[email protected]>
2021-04-15 12:52:05 -07:00

7.0 KiB

title, description, author, ms.author, ms.date, ms.topic, dev_langs, helpviewer_keywords
title description author ms.author ms.date ms.topic dev_langs helpviewer_keywords
WPF resources in code Learn about how Windows Presentation Foundation (WPF) resources, typically defined and used in XAML, can be used in code. Resources can be accessed, created, and managed in code. adegeo adegeo 04/01/2021 overview
csharp
vb
keys [WPF], using objects as
resources [WPF], accessing from procedural code
procedural code [WPF], creating resources with
procedural code [WPF], accessing resources from
resources [WPF], creating with procedural code

Resources in code (WPF .NET)

This overview concentrates on how Windows Presentation Foundation (WPF) resources can be accessed or created using code rather than XAML syntax. For more information on general resource usage and resources from a XAML syntax perspective, see Overview of XAML resources.

Accessing resources from code

The keys that identify XAML defined resources are also used to retrieve specific resources if you request the resource in code. The simplest way to retrieve a resource from code is to call either the xref:System.Windows.FrameworkElement.FindResource%2A or the xref:System.Windows.FrameworkElement.TryFindResource%2A method from framework-level objects in your application. The behavioral difference between these methods is what happens if the requested key isn't found. xref:System.Windows.FrameworkElement.FindResource%2A raises an exception. xref:System.Windows.FrameworkElement.TryFindResource%2A won't raise an exception but returns null. Each method takes the resource key as an input parameter, and returns a loosely typed object.

Typically, a resource key is a string, but there are occasional nonstring usages. The lookup logic for code resource resolution is the same as the dynamic resource reference XAML case. The search for resources starts from the calling element, then continues through parent elements in the logical tree. The lookup continues onwards into application resources, themes, and system resources if necessary. A code request for a resource will properly account for changes to those resources that happened during runtime.

The following code example demonstrates a xref:System.Windows.Controls.Primitives.ButtonBase.Click event handler that finds a resource by key, and uses the returned value to set a property.

:::code language="csharp" source="./snippets/xaml-resources-and-code/csharp/MainWindow.xaml.cs" id="ButtonBrush"::: :::code language="vb" source="./snippets/xaml-resources-and-code/vb/MainWindow.xaml.vb" id="ButtonBrush":::

An alternative method for assigning a resource reference is xref:System.Windows.FrameworkElement.SetResourceReference%2A. This method takes two parameters: the key of the resource, and the identifier for a particular dependency property that's present on the element instance to which the resource value should be assigned. Functionally, this method is the same and has the advantage of not requiring any casting of return values.

Still another way to access resources programmatically is to access the contents of the xref:System.Windows.FrameworkElement.Resources%2A property as a dictionary. Resource dictionaries are used to add new resources to existing collections, check to see if a given key name is already used by the collection, and other operations. If you're writing a WPF application entirely in code, you can also create the entire collection in code, assign resources to it. The collection can then be assigned to the xref:System.Windows.FrameworkElement.Resources%2A property of an element. This is described in the next section.

You can index within any given xref:System.Windows.FrameworkElement.Resources%2A collection, using a specific key as the index. Resources accessed in this way don't follow the normal runtime rules of resource resolution. You're only accessing that particular collection. Resource lookup doesn't traverse the resource scope to the root or the application if no valid object was found at the requested key. However, this approach may have performance advantages in some cases precisely because the scope of the search for the key is more constrained. For more information about how to work with a resource dictionary directly, see the xref:System.Windows.ResourceDictionary class.

Creating resources with code

If you want to create an entire WPF application in code, you might also want to create any resources in that application in code. To achieve this, create a new xref:System.Windows.ResourceDictionary instance, and then add all the resources to the dictionary using successive calls to xref:System.Windows.ResourceDictionary.Add%2A?displayProperty=nameWithType. Then, assign the created xref:System.Windows.ResourceDictionary to set the xref:System.Windows.FrameworkElement.Resources%2A property on an element that's present in a page scope, or the xref:System.Windows.Application.Resources%2A?displayProperty=nameWithType. You could also maintain the xref:System.Windows.ResourceDictionary as a standalone object without adding it to an element. However, if you do this, you must access the resources within it by item key, as if it were a generic dictionary. A xref:System.Windows.ResourceDictionary that's not attached to an element Resources property wouldn't exist as part of the element tree and has no scope in a lookup sequence that can be used by xref:System.Windows.FrameworkElement.FindResource%2A and related methods.

Using objects as keys

Most resource usages will set the key of the resource to be a string. However, various WPF features deliberately use the object type as a key instead of a string. The capability of having the resource be keyed by an object type is used by the WPF style and theming support. The styles and themes that become the default for an otherwise non-styled control are each keyed by the xref:System.Type of the control that they should apply to.

Being keyed by type provides a reliable lookup mechanism that works on default instances of each control type. The type can be detected by reflection and used for styling derived classes even though the derived type otherwise has no default style. You can specify a xref:System.Type key for a resource defined in XAML by using the x:Type Markup Extension. Similar extensions exist for other nonstring key usages that support WPF features, such as ComponentResourceKey Markup Extension.

For more information, see Styles, DataTemplates, and implicit keys.

See also