Gerrit ten Napel Gerrit ten Napel -4 years ago 39
C# Question

How do I create a pop up window containing a canvas?

I'm creating a little paint program and I want to have a canvas pop up when I click on a button.

private void buttonNewDrawing_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(mainCanvas);
}


Doesn't work obviously because it's asking for a string. but what can I then use instead of a
MessageBox
to just have the canvas
mainCanvas
pop up so I can use it?

Thank you in advance.

Answer Source

MainWindow.xaml:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button Click="Button_Click" Grid.Row="2" HorizontalAlignment="Center" HorizontalContentAlignment="Center" VerticalAlignment="Bottom" VerticalContentAlignment="Center" Content="Click"/>
    </Grid>
</Window>

MainWindow.xaml.cs:

private void Button_Click(object sender, RoutedEventArgs e)
{
    Window1 window = new Window1();
    window.Show();
}

Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <Canvas />
    </Grid>
</Window>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download