hazz

I thought I could add a panel (as I did in .NET 2.0 windows forms) to a Grid but get the error;

cannot convert from 'System.Windows.Forms.Panel' to 'System.Windows.UIElement'

for the following. The line of code that causes the error is in red at bottom. This worked fine for adding a button but not a panel. Thank you. -Greg

Code Snippet

using System;

using System.Windows;

using System.Windows.Controls;

class SplitTheClient : Window

{

[STAThread]

public static void Main()

{

Application app = new Application();

app.Run(new SplitTheClient());

}

public SplitTheClient()

{

Title = "Split the Client";

Grid grid = new Grid();

this.Content = grid;

grid.ColumnDefinitions.Add(new ColumnDefinition());

grid.ColumnDefinitions.Add(new ColumnDefinition());

grid.ColumnDefinitions.Add(new ColumnDefinition());

grid.ColumnDefinitions[1].Width = new GridLength(6.0);

grid.RowDefinitions.Add(new RowDefinition());

grid.RowDefinitions.Add(new RowDefinition());

grid.RowDefinitions.Add(new RowDefinition());

grid.RowDefinitions[1].Height = new GridLength(6.0);

// the horizontal splitter in Row 1

GridSplitter hsplit = new GridSplitter();

hsplit.ShowsPreview = true;

hsplit.HorizontalAlignment = HorizontalAlignment.Stretch;

hsplit.VerticalAlignment = VerticalAlignment.Stretch;

Grid.SetRow(hsplit, 1);

Grid.SetColumnSpan(hsplit, 3);

grid.Children.Add(hsplit);

// the vertical splitter in Column 1

GridSplitter vsplit = new GridSplitter();

vsplit.ShowsPreview = true;

vsplit.HorizontalAlignment = HorizontalAlignment.Stretch;

vsplit.VerticalAlignment = VerticalAlignment.Stretch;

Grid.SetColumn(vsplit, 1);

Grid.SetRowSpan(vsplit, 3);

grid.Children.Add(vsplit);

// some sample content

for (int x = 0; x < 3; x += 2)

{

for (int y = 0; y < 3; y += 2)

{

// Button button = new Button();

System.Windows.Forms.Panel p = new System.Windows.Forms.Panel();

//button.Content = x + ", " + y;

//Grid.SetColumn(button, x);

//Grid.SetRow(button, y);

//grid.Children.Add(button);

Grid.SetColumn(p, x);

Grid.SetRow(p, y);

grid.Children.Add(p);

}

}

}

}



Re: Windows Presentation Foundation (WPF) cannot convert from 'System.Windows.Forms.Panel' to 'System.Windows.UIElement'

Josh Smith

Put a WindowsFormsHost into the Grid, and then put the Button into the host. But, why are you using a WinForms Button in a WPF app in the first place Are you aware that WPF has a Button control




Re: Windows Presentation Foundation (WPF) cannot convert from 'System.Windows.Forms.Panel' to 'System.Windows.UIElement'

hazz

Thanks for the WindowsFormHost idea. I do want to use a panel rather than a button.

The button worked but the panel does not. -greg





Re: Windows Presentation Foundation (WPF) cannot convert from 'System.Windows.Forms.Panel' to 'System.Windows.UIElement'

WPCoder

The button worked as that was a WPF button.

Try a Canvas if you want something that acts like a WinForm panel -- both are absolute layout controls.

If you need the WinForm Panel -- would you mind saying why It helps MS understand what people are actually trying to do with WPF (and sometimes others can help you find alternatives as available).






Re: Windows Presentation Foundation (WPF) cannot convert from 'System.Windows.Forms.Panel' to 'System.Windows.UIElement'

hazz

Thanks Aaron!

Now I can try to figure out how to make the selected canvas's border standout.

How is the panel/canvas being used

As a vb.net container to handle opengl after sending the hwnd to an unmanaged c++ dll..... similar to this link I just saw

http://www.codeproject.com/managedcpp/OpenGLViewWinForms.asp