Is it possible to include XAML files into another XAML file?

S

star-italia

Hi, Is it possible to include a XAML file into another XAML file?

For example If i have
- Window1.xaml
- Window2.xaml

and both have a treeview with a custom Style, can i describe the style
in a trvStyle.xaml and then include it in both windows?

Thanks in advance
 
S

star-italia

Tom said:
I don't know of anyway to do that (that doesn't mean there isn't :)-
but If this is the same application, you could define the style
in the application resources, and then it will be available globablly.

And what if i want to create a control in XAML and then reuse it in
another window?

And to be more precise, I'm loading XAML on the fly, so I'd not want to
load it as a resource. However thanks for your reply :)
 
S

star-italia

Tom said:
If your loading it on the fly, then you would have to use some procedural
code, and do something like this...

Create the trvStyle.xaml:
<ResourceDictionary
xmlns="http://......"
xmlns:x="http://...." >
<Style x:Key="TreeViewStyle" TargetType="{x:Type TreeView}">
<!-- do your style stuff here -->
</Style>
</ResourceDictionary>

<Window x:Name="Window1" ...>
...
<TreeView Style="{DynamicResource TreeViewStyle}">
...
</TreeView>
</Window>

Then you need to load the style dynamically (XamlReader.Load) the resource
dictionary and then either replace the Application.Current.Resources (or what
ever ResourceDictionary your interested in) or you could merge them together.
Adding or replacing the enteries with those from the dynamically loaded
dictionary.

HTH

Thank you for your help
 
J

Jialiang Ge [MSFT]

Hello star-italia and Tom,

XamlReader.Load() can do the task when the resource dictionary is in a
loose XAML form. However, if the resource dictionary is compiled into
BAML, we need to use the Application.LoadComponent() method to de-serialize
it into ResourceDictionary object, then add it to either
Application.Current.Resources or Window.Resources dictionary through the
MergedDictionaries property:

ResourceDictionary resourceDictionary = Application.LoadComponent(new
Uri("CommonResources.xaml", UriKind.Relative)) as ResourceDictionary;
if (resourceDictionary != null)
{

Application.Current.Resources.MergedDictionaries.Add(resourceDictionary);
}

When the resource is merged into the Application level, we use
StaticResource or DynamicResource to refer to them in either Window1.xaml
or Widnow2.xaml files.
The code above can also be written in Window1 or Window2 constructor at
codebehind as follows:
ResourceDictionary resourceDictionary = Application.LoadComponent(new
Uri("CommonResources.xaml", UriKind.Relative)) as ResourceDictionary;
if (resourceDictionary != null)
{
this.Resources.MergedDictionaries.Add(resourceDictionary);
}
In this way, the window will have a separate copy of the resource
dictionaries, though this might not be very efficient considering the
consumption of the memory.

If you plan to merge resources completely in XAML, we can directly specify
the MergedDictionary in XAML as follows:
<App.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack uri to the resource dictionary xaml
file"/>
</ResourceDictionary.MergedDictionaries
</ResourceDictionary>
</ App.Resources>
One caveat here is that you define the Visuals like Button in the resource
dictionary as follows:
<Button x:Key="button"/>
Because one Visual can have only one visual parent, we cannot re-parented
it for multiple times in the visual tree, so that if the "button" resource
is going to be reused in multiple places, please specify the x:Shared
property to "False" when declaring the Button resource as this:
<Button x:Shared="False" x:key="button"/>
x:Shared means that, every time this "button" resource is applied, WPF will
create a unique copy. But when x:Shared is specified, resource dictionary
should be compiled into BAML beforehand. So if you are using loose,
non-compiled XAML, please ignore my suggestion.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
L

Linda Liu[MSFT]

Hi Star-italia,

How about the problem now?

If you have any question, please feel free to let us now.

Thank you for using our MSDN Managed Newsgroup Support Service!

Sincerely,
Linda Liu
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top