user3033921
user3033921

Reputation: 209

WPF using view inside mainwindow

I am trying to use a view inside my mainwinow. So I created the view inside the view folder in my project and try to use it in the main window. But I get run time error. Can anyone help?

XAML MainWindow

<Window x:Class="DataRetrieval.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:prism="http://www.codeplex.com/prism"
         xmlns:views="clr-namespace:DataRetrieval.Views"
        Title="MainWindow" Height="350" Width="525">
    <TabControl Name="TabControl1" >
        <TabControl.ItemContainerStyle>
            <Style TargetType="{x:Type TabItem}">
                <Setter Property="Visibility" Value="Collapsed"/>
            </Style>
        </TabControl.ItemContainerStyle>
        <TabItem Header="General">
            <views:Login></views:Login>
        </TabItem>

    </TabControl>
</Window>

XAML View:

<Window x:Class="DataRetrieval.Views.Login"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Login" Height="300" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>


        </Grid.ColumnDefinitions>
        <StackPanel Grid.Column="0">
            <Label Content="User Name:" Width="Auto"/>
            <Label Content="Password:" Width="Auto"/>

        </StackPanel>
        <StackPanel Grid.Column="1">
            <TextBox Grid.Column="1" Name="Username"  Text="" VerticalAlignment="Top" Width="Auto"/>
            <PasswordBox Grid.Column="1" Name="Password" VerticalAlignment="Top" Width="Auto"/>

        </StackPanel>

    </Grid>
</Window>

But when I run the application, I get a runtime error on my mainwindow xaml code:

A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll

Additional information: 'Set property 'System.Windows.Controls.ContentControl.Content' threw an exception.' Line number '14' and line position '14'.

Upvotes: 2

Views: 4712

Answers (3)

GeorgDangl
GeorgDangl

Reputation: 2192

You might be able to embed it in a frame:

<Frame  Source="Login.xaml" />

instead of

<views:Login></views:Login>

You might need to make the Login.xaml Window a Page though.

I'm sure there are better ways to achieve it, but it works=)

Upvotes: 1

Thabo
Thabo

Reputation: 1554

Change the second window to user control.Then it will work

Upvotes: 3

Domysee
Domysee

Reputation: 12854

You cannot nest a Window in another Window. You can either put the Grid directly in the Window or create a UserControl instead.

Upvotes: 3

Related Questions