Kamal Rathnayake
Kamal Rathnayake

Reputation: 582

Access created UI Elements in XAML WPF

I Have

class Canvas2:Canvas
{
}

class created in the same namespace. i can't use Canvas2 in XAML. how can i make Canvas2 accessable in XAML code? Im a newby.

Upvotes: 0

Views: 125

Answers (3)

Martini Bianco
Martini Bianco

Reputation: 1795

A quick and dirty answer: Add the following line to your AssemblyInfo.cs

[assembly: XmlnsDefinition("http://schemas.microsoft.com/winfx/2006/xaml/presentation", "YourNamespace")]

This way you add all classes in your Namespace to the default WPF-XML-Namespace. Then you can use your classes directly without adding an custom xml namespace.

Warning: Even if this works, it is not the recomended way. Especially I would not recommend this for larger projects because it could easily lead to name conflicts.

The correct way is to add a custom XML namespace like nit and AsitK described.

Upvotes: 0

Nitin Purohit
Nitin Purohit

Reputation: 18580

Define the xmlns like xmlns:local="clr-namespace:WpfApplication1" assuming Canvas2 is define in namespace WpfApplication1.

then you can use Canvas2 as <local:Canvas2 x:Name="MyCanvas"/>

Thanks

Upvotes: 1

AsitK
AsitK

Reputation: 673

you have to import your local namespace to the XAML too. Check the MSDN for sample.

Upvotes: 0

Related Questions