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.

7
  • 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? Commented Aug 18, 2013 at 8:05
  • @MitchWheat i want to create a Panel/ Commented Aug 18, 2013 at 8:11
  • @ZoyaSheikh Maybe you want StackPanel or Canvas? They're both non abstract Panels. Commented Aug 18, 2013 at 8:15
  • @MitchWheat-Joachim want DockPanel. Commented Aug 18, 2013 at 8:18
  • there are a lot of Panels that you can create Commented Aug 18, 2013 at 8:24

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

3 Comments

you could give a reference to these
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?
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?
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 Comment

I don't think there is any need to derive a new class from Panel when there already are so many.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.