Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Window.Content to a page by XAML?

Tags:

window

wpf

xaml

<Window x:Class="MyWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:src="clr-namespace:WpfApplication1"
    Title="ContactsSelector" Height="300" Width="300">
    <Window.Content>
        <src:MyPage>
           <!--MyPage is a page that I created and exists in the project--> 
        </src:MyPage>
    </Window.Content>
</Window>

I want to set the content of a window to a page, just like I would do it programmatically:

Dim w As New MyWindow
Dim p As New MyPage
w.Content = p
w.ShowDialog()

Or set it in the Load event of the window, summarily I want it to be done in xaml.

like image 727
Shimmy Weitzhandler Avatar asked Sep 03 '09 01:09

Shimmy Weitzhandler


People also ask

How do I change the content of a WPF window?

You can put the content of the window into a UserControl. Your window then only has a content-control and a button to change the content. On a click on the button you can reassign the content-property of the content-control. I've made a small example for this.

Can I use XAML in Windows Forms?

Hosting the Windows Forms Controlxaml in the WPF Designer. In the Window element, add the following namespace mapping. The wf namespace mapping establishes a reference to the assembly that contains the Windows Forms control. In the Grid element add the following XAML.

What is window in XAML?

Window is the root window of XAML applications which provides minimize/maximize option, title bar, border, and close button. It also provides the ability to create, configure, show, and manage the lifetime of windows and dialog boxes.

How do I change the startup window in WPF?

If you look at App. xaml class of your WPF application, you will see the following XAML code. Here the StartupUri sets the startup Window of an application. If you want to change the Startup window to some other window, just change this value.


2 Answers

Use a Frame element to show the content of the page.

<Window> <Frame Source="/Pages/MyPage.xaml"/> </Window>
like image 67
Marc Avatar answered Nov 15 '22 08:11

Marc


Try something like this, where MyPageAssembly points to the assembly where your page resides, and MyPage is the name of the Page.

<Window 
    x:Class="MyWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:MyPageAssembly="clr-namespace:MyPage;assembly=MyPageAssembly"
    Title="ContactsSelector" 
    Height="300" 
    Width="300"
    >
    <Window.Content>
        <MyPageAssembly:MyPage />
    </Window.Content>
</Window>
like image 28
user112889 Avatar answered Nov 15 '22 06:11

user112889