473,396 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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
6 9987
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Blah Blah | last post by:
here's my problem - i run a web site with a java servlet backend (apache/tomcat/linux/mysql), and i want to add some php content to my jsp pages. why would i want to do something like this? i...
11
by: Ioannis Vranos | last post by:
Will XAML be mandatory in VS "Orcas"? -- Ioannis Vranos
4
by: Thilaka | last post by:
Hi guys, Recently i read some articles on Longhorn & XAML. & From what i learnt the future for developing UIs lies in XAML. Does this mean that I've got to learn XAML as developing UIs for web...
9
by: Wayne Smith | last post by:
I've come up against a major headache that I can't seem to find a solution for but I'm sure there must be a workaround and I would really be grateful of any help. I'm currently building a web...
6
by: moondaddy | last post by:
will CodeDom create XAML windows or just regular windows forms? If so, how to I tell it to create a xaml window instead of a windows form? thanks -- moondaddy@noemail.noemail
0
by: =?Utf-8?B?WmlnZ3lTaG9ydA==?= | last post by:
I have a sample XAML file: <StackPanel xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <Button Name="button2" Height="23" Width="300" Margin="31,37,0,0" Click="doit">Hello...
2
by: unclepony | last post by:
I just want to make application like a Expression blend but it is not easy for me. And I already know that we can make custom form designer control that behave like visual studio. Microsoft...
6
by: tgnelson85 | last post by:
Hello, C question here (running on Linux, though there should be no platform specific code). After reading through a few examples, and following one in a book, for linked lists i thought i would...
4
by: moondaddy | last post by:
I have a user control located in the following path: projectFolder/PropertyControls/myControl.xaml and in myControl.xaml I have a reference to a resource dictionary like this: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.