How do I create a pop up window containing a canvas? - c#

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.

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>

Related

How to navigate between windows in WPF? (not browser)

I want to develop a desktop application based on WPF. How do I navigate through C # code from one window to another in a window? in the other word, I have tried to set up a click event for a radiobutton that opens another window in frame or border.
<Window x:Class="WpfNavigation2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfNavigation2"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" Closing="Window_Closing">
<Grid>
<Border Margin="10,100,10,10" Background="AliceBlue">
<Grid Name="contergrid">
</Grid>
</Border>
<RadioButton x:Name="radioButton" Content="RadioButton1" HorizontalAlignment="Left" Margin="60,0,0,0" VerticalAlignment="Top" Click="btn1"/>
<RadioButton x:Name="radioButton1" Content="RadioButton2" HorizontalAlignment="Left" Margin="60,37,0,0" VerticalAlignment="Top" Click="btn2"/>
<RadioButton x:Name="radioButton2" Content="RadioButton3" HorizontalAlignment="Left" Margin="60,68,0,0" VerticalAlignment="Top" Click="btn3"/>
</Grid>
</Window>
private void btn1(object sender, RoutedEventArgs e)
{
//open window 1 in border
}
What you are looking for is a ContentPresenter. You can put all kinds of content in a ContentPresenter for example a control
<Border Margin="10,100,10,10" Background="AliceBlue">
<ContentPresenter Name="contentFrame">
</ContentPresenter>
</Border>
private void btn1(object sender, RoutedEventArgs e)
{
Grid grd = new Grid();
grd.Background = new SolidColorBrush(Colors.HotPink);
contentFrame.Content = grd; // replace me with your control / page / window1
}

Change entire window view in wpf [duplicate]

This question already has answers here:
If the user logs on successfully, then I want to show the main window, if not, I want to exit the application
(3 answers)
Closed 4 years ago.
I have a main window that I created and it looks like usual log in screen.
Once the user click on a button I would like the whole window to change.
In all the examples I saw either I have to keep a stackpanel/dock and then only the frame changes.
For example, I have the following Main window xaml:
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Frame x:Name="Main" Margin="0,35,0,0"/>
<Button Content="Button" Click="Button_Click" Margin="0,105,326,100"/>
</Grid>
</Window>
and the new page:
<Page x:Class="WpfApp1.Optimizer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Optimizer">
<Grid Background="Black">
<Button Content="Button" HorizontalAlignment="Left" Height="91" Margin="125,87,0,0" VerticalAlignment="Top" Width="129"/>
</Grid>
</Page>
And my command is:
private void Button_Click(object sender, RoutedEventArgs e)
{
Main.Content = new Optimizer();
}
In that case the two views appear on same page instead of switching completely.
Its not the case im looking for. I need the whole window to change and not leave behind a stackpanel.
Any simple example would be appreciated.
First thing to mention here is that pages and frames are rarely used by commercial teams. At least not in my experience.
Unless writing xbap - which is wpf in a browser.
I suggest you consider usercontrols rather than pages and host them in contentcontrols.
You can then have something like:
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Click="Button_Click"/>
</Grid>
</Window>
Click handler:
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Content = new UserControl1();
}
That makes the entire content of mainwindow an instance of UserControl1.
A Window inherits from contentcontrol.
You should also have learning mvvm on your list.
You should learn that. Maybe not right now, but soon.

WPF page content not setting to window's size

I made a WPF application in which. I placed a rectangle. The page is covering whole window but the size(width) of rectangle is not equal to the page.
here is the pic.
here is the source code of my application:
<Page x:Class="TestWpfApplication.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TestWpfApplication"
mc:Ignorable="d"
d:DesignHeight="350" d:DesignWidth="525"
Title="Page1" Background="#FF7ACBBC" ShowsNavigationUI="False">
<Grid>
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="100" Stroke="Black" VerticalAlignment="Top" Width="525"/>
<Label x:Name="label" Content="Heading" HorizontalAlignment="Left" Margin="222.006,25,0,0" VerticalAlignment="Top" FontFamily="Tahoma" FontSize="22"/>
<TextBlock x:Name="textBlock" HorizontalAlignment="Left" Margin="160,155,0,0" TextWrapping="Wrap" Text="And rest of the content goes here" VerticalAlignment="Top" FontFamily="Tahoma" FontSize="14"/>
</Grid>
And
<Window x:Class="TestWpfApplication.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:TestWpfApplication"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<local:Page1/>
</Grid>
At last App.xaml
<Application x:Class="TestWpfApplication.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestWpfApplication"
StartupUri="page1.xaml">
<Application.Resources>
</Application.Resources>
Please help me....
Set SizeToContent Property of Window to SizeToContent="WidthAndHeight"
SizeToContent
Page1.Xaml.cs:
public Page1()
{
InitializeComponent();
this.SizeToContent = SizeToContent.WidthAndHeight;
}
For more Info, See Window.SizeToContent Property
I don't know why you specified size parameters for rectangle and expect it to fit your page's size. Just remove all these parameters and it will stretch.
<Rectangle Fill="#FFF4F4F5"/>

Media element not support in WPF Page

I want to display a progress bar in WPF Page,when it loaded. I used media element to display a gif image. Below code describe what I did.
<Page x:Class="WpfApplication3.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1" Loaded="Page_Loaded">
<Grid>
<MediaElement Grid.Column="2" Grid.Row="4" Margin="0,0,47,0" HorizontalAlignment="Right" Width="80" Source="progress.gif" LoadedBehavior="Manual" Name="MP"/>
</Grid>
</Page>
private void Page_Loaded(object sender, RoutedEventArgs e)
{
MP.Play();
}
but this progress bar not showing in Page
How can I fix this.
(when I tried with progress bar also, got same problm)
(It works ok in window )
You could use the WpfAnimatedGif library to easily display an animated gif using a simple Image element:
How do I get an animated gif to work in WPF?
Just download the package from NuGet: https://www.nuget.org/packages/WpfAnimatedGif
And set the ImageBehavior.AnimatedSource attached property of the Image element:
<Page x:Class="WpfApplication3.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
Title="Page1"
<Grid>
<Image gif:ImageBehavior.AnimatedSource="progress.gif" />
</Grid>
</Page>

C# WPF UserControls are not in the windows

the MainWindow Code is like this:
<Window x:Class="UserGUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UserGUI"
mc:Ignorable="d"
Title="MainWindow" Height="657.278" Width="952.33" ResizeMode="CanMinimize">
<Canvas>
<ContentControl Name="mainContentControl" Grid.Column="0" Height="585" Width="934" Canvas.Top="31"/>
<ContentControl Name="upperMenu" Grid.Column="1" Height="31" Width="944"/>
</Canvas>
and the xaml is:
public MainWindow()
{
InitializeComponent();
mainContentControl.Content = new AdminMenu();
upperMenu.Content = new LoginScreen();
}
But I do not see the user controls in my mainwindow...
It seems they are not aligned at all
What am i doing wrong?
The LoginScreen Canvas is:
<Canvas x:Name="loginCanvas" Background="BlanchedAlmond" Height="620" VerticalAlignment="Top" HorizontalAlignment="Left" Width="934">
and the window of the userControl window itself is:
Height="630" Width="944">
Hope i'm clear enough
There are some strange things in your XAML. There is no Grid and no columns, but you have some positioning based on it. Try this:
<Window x:Class="UserGUI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UserGUI"
Title="MainWindow" Height="657.278" Width="952.33" ResizeMode="CanMinimize">
<Canvas>
<ContentControl Name="mainContentControl" Height="585" Width="934" Canvas.Top="31" Canvas.Left="0"/>
<ContentControl Name="upperMenu" Canvas.Top="0" Canvas.Left="0" Height="31" Width="944"/>
</Canvas>

Categories

Resources