views:

509

answers:

2

I have a problem with figuring out how to refer to a Canvas in another XAML file in my project, for example there is a Canvas object containing some labels, images called MyLayout and is stored in MyLayout.xaml I want to use this Canvas in the main Window of the application in Window.xaml - how can this be done, as there will be multiple layouts that will be needed to be loaded into the Window - all XAML is within the project and compiled it cannot be loaded from a file - how to I reference the Canvas Object in the MyLayout.xaml file in the Window.xaml this can be in XAML, VB.NET code or even C#. I have searched for hours trying to figure this out - how to use an object from one XAML file in another - how is this done?

Thanks in advance - hope someone knows how to do this

+1  A: 

Have you thought about using a UserControl or ControlTemplate?

bryanbcook
A: 

Since your Canvas object is really a subclass of Canvas, that subclass is defined in your assembly. You can reference objects defined in other namespaces by adding another xmlns attribute to the root XAML object.

Something like this (assuming that your Canvas subclass is called MyCanvas in the MyNamespace namespace):

<Window x:Class="MyNamespace.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:MyNamespace">
    <local:MyCanvas />
</Window>

You can set the properties of the MyCanvas class like any other object.

Andy