Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How To: Subclass User Control
Message
General information
Forum:
ASP.NET
Category:
Windows Presentation Foundation (WPF)
Miscellaneous
Thread ID:
01470880
Message ID:
01471223
Views:
57
>I'm trying to create a user control that I can use as a base for other user controls. So created this:
>
>
>public abstract partial class _ViewBase : UserControl
>{
>    public _ViewBase()
>    {
>        InitializeComponent();
>    }
>
>    public abstract void Add();
>    public abstract void Edit();
>    public abstract void Remove();
>    public abstract void Save();
>}
>
>
>The I get the comp error:
>
Partial declarations of 'UserView' must not specify different base classes
>
>So I Googled it, found and followed this: http://geekswithblogs.net/lbugnion/archive/2007/03/02/107747.aspx
>
>Now I get:
>
>
>"_ViewBase" cannot be the root of a XAML file because it was defined using XAML. 
>
>
>How in the world do you subclass a user control in WPF???

Not sure how the link helps but there's a few things you have to do. Firstly the base class must be defined entirely in C# as a (AFAIK a non-abstract) class so something like :
namespace Junk
{
    public class _ViewBase : UserControl
    {
        public void Add() { }
        public void Edit() { }
        public void Remove() { }
        public void Save() { }
    }
}
To use this in as a subclassed control:
<b:_ViewBase x:Class="Junk.MyViewBase"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:b="clr-namespace:Junk">
</b:_ViewBase>
with C#:
namespace Junk
{
    public partial class MyViewBase : _ViewBase
    {
        public MyViewBase()
        {
            InitializeComponent();
        }
    }
}
The first error you got would have been because you didn't sync the base class name between the C# and XAML in the derived class and the second because you tried to create your base class using a XAML component....
HTH
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform