Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How To: Subclass User Control
Message
Information générale
Forum:
ASP.NET
Catégorie:
Windows Presentation Foundation (WPF)
Divers
Thread ID:
01470880
Message ID:
01509579
Vues:
43
Hi,
To just answer the question: Since you can't have XAML you would have to do it in code. e.g:
public _ViewBase()
        {
            StackPanel sp = new StackPanel();
            Button b = new Button();
            b.Content = "Click me";
            sp.Children.Add(b);
            this.Content = sp;
        }
But I agree with John - from a WPF POV this is very unlikely to be the right approach. Are you using MVVM for this app?

>Revisiting this topic...
>
>I get how to subclass the control. The question is how do you put a button on the base class?
>
>Thanks
>
>
>>>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
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform