WPF FAQ - Resources

Find answers for the most frequently asked questions
Expand All Collapse All

Embedded XAML Resources

Several WPF assemblies come with embedded XAML resources, like template definitions, style definitions, etc. And it’s often very useful to take a look at these embedded XAML to understand the structure of a control and thereby making it easier to customize it’s UI in your applications, if necessary.

The best way to take a look at these embedded XAML resources is to use the Reflector tool with the BAML Viewer plugin.

Reflector + BAML Viewer

Reflector is a popular Free tool available since the early days of .NET to help you take a deeper look at the source and resources comprising a .NET assembly. Reflector can be downloaded here:

.NET Reflector

Here is a thumbnail view of the Reflector tool:

Once you install the Reflector, download the BAML Viewer plug-in from:
BAML Viewer – Reflector Plugin

The plug-in is a single dll called Reflector.BamlViewer.dll. Put this dll next to the Reflector.exe and follow these steps to add the plug-in to the Reflector:
a) In the Reflector tool, select the View/Add Ins… menu item.
b) In the ‘Add Ins’ dialog box, Add the above dll as an ‘add-in’. This will include a new ‘Tools/BAML Viewer’ menu item in the Reflector.
c) Then open a WPF assembly (or Syncfusion assembly) that usually contains a BAML resource, like PresentationFramework.Aero.dll (usually found under ‘%ProgramFiles%\Reference Assemblies\Microsoft\Framework\v3.0\PresentationFramework.Aero.dll’)
d) Then select the ‘Tools/BAML Viewer’ menu item. This will open a new ‘BAML Viewer’ view showing the embedded XAML in the above assembly.

Here is a screenshot of the BAML Viewer showing the embedded XAML in the PresentationFramework.Aero.dll:

You can similarly view XAML resources embedded into Syncfusion assemblies.

Permalink

The following example shows an application definition file. The application definition file defines a resource section (a value for the Resources property). Resources defined at the application level can be accessed by all other pages that are part of the application. In this case, the resource is a declared style. Because a complete style that includes a control template can be lengthy, this example omits the control template that is defined within the “ContentTemplate” property setter of the style.

[XAML]
<Application.Resources>
  <Style TargetType='Button' x:Key='GelButton' >
    <Setter Property='Margin' Value='1,2,1,2'/>
    <Setter Property='HorizontalAlignment' Value='Left'/>
    <Setter Property='Template'>
      <Setter.Value>

	...

      </Setter.Value>
    </Setter>
  </Style>
</Application.Resources>

The following example shows an XAML page that references the application level resource that the previous example defined. The resource is referenced by using a StaticResource Markup Extension that specifies the unique resource key for the requested resource. No resource with the key of ‘GelButton’ is found in the current page, so the resource lookup scope for the requested resource continues beyond the current page and into the defined application level resources.

[XAML]

<StackPanel
  Name='root'
  xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'
  xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'>
<Button Height='50' Width='250' Style='{StaticResource GelButton}' Content='Button 1' />
<Button Height='50' Width='250' Style='{StaticResource GelButton}' Content='Button 2' />
</StackPanel>
Permalink

Share with

Couldn't find the FAQs you're looking for?

Please submit your question and answer.