2

I wrote this code in WPF:

   System.Windows.Controls.Panel Panel1 = null;
   Panel1 = new System.Windows.Controls.Panel();

But it is saying that:

Error1 Cannot create an instance of the abstract class or interface 'System.Windows.Controls.Panel'

How to solve this? Can anyone answer my query.

Zoya Sheikh
  • 871
  • 3
  • 11
  • 21
  • 6
    The error message is correct! You simply Cannot create an instance of any abstract class or interface. What are you actually trying to do? – Mitch Wheat Aug 18 '13 at 08:05
  • @MitchWheat i want to create a Panel/ – Zoya Sheikh Aug 18 '13 at 08:11
  • @ZoyaSheikh Maybe you want `StackPanel` or `Canvas`? They're both non abstract `Panel`s. – Joachim Isaksson Aug 18 '13 at 08:15
  • @MitchWheat-Joachim want DockPanel. – Zoya Sheikh Aug 18 '13 at 08:18
  • there are a lot of [Panels](http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx#inheritanceContinued) that you can create – default Aug 18 '13 at 08:24
  • @MitchWheat There is DockPanelA in my MainWindow.xaml. I defined MVVM framework to edit, delete and to update data in grid. I want to create new PanelB in my ViewModel Class, whenever a button named as "New" will be clicked. PanelA will get hide and PanelB should take position of Panle A? – Zoya Sheikh Aug 18 '13 at 08:31
  • @Zoya Sheikh - That's entirely separate question. Raise another question and post your relevant XAML code there. Not forgot to add what have you tried and what issue you facing while implementing it. – Rohit Vats Aug 18 '13 at 08:34

2 Answers2

5

From MSDN -

Abstract classes cannot be instantiated, and are frequently either partially implemented, or not at all implemented.

If you need Panel, create an object of classes deriving from Panel. Most popular are

  • Grid
  • DockPanel
  • StackPanel
  • Canvas

Complete list for classes deriving from Panels can be found here.

This will serve your purpose -

System.Windows.Controls.Panel Panel1 = new System.Windows.Controls.DockPanel();

But I think you need to access Dock property of DockPanel (and properties specific to DockPanel), so you should create DockPanel object instead -

System.Windows.Controls.DockPanel Panel1 = new System.Windows.Controls.DockPanel();
Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
  • you could give a reference to [these](http://msdn.microsoft.com/en-us/library/system.windows.controls.panel.aspx#inheritanceContinued) – default Aug 18 '13 at 08:24
  • There is DockPanelA in my MainWindow.xaml. I defined MVVM framework to edit, delete and to update data in grid. I want to create new PanelB in my ViewModel Class, whenever a button named as "New" will be clicked. PanelA will get hide and PanelB should take position of Panle A? – Zoya Sheikh Aug 18 '13 at 08:29
  • Create a `ContentControl` and two `DataTemplates`. On button click change the `DataTemplates`. However, how that's related to creating an instance of an abstract class? – Rohit Vats Aug 18 '13 at 08:32
1

You can't create instance of abstract class see abstract keyword

You can derive from abstract class and then you can user your derived objces as the abstract type, see polymorphism and inheritance

  • 1
    I don't think there is any need to derive a new class from `Panel` when there already are so many. – default Aug 18 '13 at 08:36