i'd like to create a class that inherits from a WPF control say UserControl or Grid and then have another XAML whose code behind in turn inherits from that class. On using the name of the custom class in the Root element of the XAML, get an error saying this is an invalid root element. Is there a way to get around this

Making custom user control as root of xaml
quiklearner
Here are the classes and XAML files: Get an error in View1.xaml saying that the root element is invalid.
Window1.xaml
<Window x:Class="WindowsApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowsApplication1" Height="300" Width="300"
xmlns:myviews="clr-namespace:WindowsApplication1"
>"
<myviews:View1>
</myviews:View1>
</Window>
--------
View1.xaml
<View1 x:Class="WindowsApplication1.View1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<StackPanel>
<Button> Test 1</Button>
<Button> Test 2</Button>
</StackPanel>
</Grid>
</View1>
-----------------------
View1.xaml.cs
namespace WindowsApplication1
{
public partial class View1 : MyView
{
public View1()
{
InitializeComponent();
}
}
}
---------------------
MyView.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;
using System.Windows;
namespace WindowsApplication1
{
public class MyView:Grid
{
}
}
------------------
c_shah
There is no problem to inherit from wpf controls or any class, it may be the case that you want to use container control but isntead of container you'r using any other control, and may be that why xaml throughs error of invalid root element.
if its not work please give more detail of your error.
Cheers
Ross Watson
connect2sandeep
There's a limitation in v1 that you cannot use a custom class as a root element, if that custom class is defined within the same assembly that is attempting to reference it. With the existing design, the XAML reader doesn't have a completed assembly to examine at the time that it tries to resolve the custom tag at the root. That's not a problem for elements under the root because there's logic in there that defers evaluation of other custom elements (really, the whole page content is deferred, as binary XAML), but the root tag is special because it needs to be processed for the partial class (even if you don't claim an x:Class), thus can't be deferred.
The workaround is to use a separate assembly to define any custom class that you plan to use as a root element, then reference that assembly in the mapping.
See http://msdn2.microsoft.com/en-gb/library/ms747086.aspx