lte__ lte__ - 16 days ago 5
C# Question

C# WPF - MainWindow content gets shown in two separate windows instead of one

I want to have a single

MainWindow
with two columns, a narrow one on the left and a wide one on the right. In the left one should be a menubar and in the right one a grid of
Fields
(which is a custom
ViewModelBase
). However, right now when I launch the program, it gives me two windows, one with the grid and one with the menubar on the left. What am I doing wrong? Where might be the error?

Tha XAML looks like this:

<Window x:Class="SavageMaci.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test" Height="500" Width="700">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<Menu Grid.Row="1" Grid.Column="1" Margin="38,0,187,430" Background="White">
<Menu.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="90"/>
<TranslateTransform/>
</TransformGroup>
</Menu.RenderTransform >
<MenuItem Header="Level 1" FontFamily="Roboto" Height="32" Width="65"/>
<MenuItem Header="Level 2" FontFamily="Roboto" Height="32" Width="65"/>
<MenuItem Header="Level 3" FontFamily="Roboto" Height="32" Width="65"/>
</Menu>
<ItemsControl Grid.Row="1" Grid.Column="2" ItemsSource="{Binding Fields}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="{Binding Size}" Columns="{Binding Size}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</Grid>
</Window>


The xaml.cs is also basic:

namespace SavageMaci
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}


There's only one reference to
MainWindow()
, in
App.xaml.cs
, where I instantiate it. Please let me know where else might the error be, I'll update the question with that code, too. Thank you for helping!

Answer

The solution to your problem is simple. In your App class, you create a MainWindow instance.

This is unnecessary since App creates its own instance whether you create it or not.

Have a look at the App.xaml file and you will see for yourself.

Comments