By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,369 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 2008-06-06, star-italia <st*********@community.nospamwrote:
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
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.

--
Tom Shelton
Jun 27 '08 #2

P: n/a
Tom Shelton wrote:
On 2008-06-06, star-italia <st*********@community.nospamwrote:
>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

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 :)
Jun 27 '08 #3

P: n/a
On 2008-06-06, star-italia <st*********@community.nospamwrote:
Tom Shelton wrote:
>On 2008-06-06, star-italia <st*********@community.nospamwrote:
>>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

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 :)
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

--
Tom Shelton
Jun 27 '08 #4

P: n/a
Tom Shelton wrote:
On 2008-06-06, star-italia <st*********@community.nospamwrote:
>Tom Shelton wrote:
>>On 2008-06-06, star-italia <st*********@community.nospamwrote:
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
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 :)

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
Jun 27 '08 #5

P: n/a
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.A dd(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(resourceDict ionary);
}
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 (ji****@online.microsoft.com, 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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Jun 27 '08 #6

P: n/a
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:
ms****@microsoft.com.

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

Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.