Can't put a window in a style (WPF)
c#, wpf
Solution
The `Window` Class:
Provides the ability to create, configure, show, and manage the lifetime of windows and dialog boxes.
and:
is primarily used to display windows and dialog boxes for standalone applications.
As `Window` elements are top-level elements, they cannot be added into the `Content` of lower level elements. Your Can't put a Window in a Style error is clear... you cannot use a `Window` in a `Style`, or in a `DataTemplate` in your case.
Instead of attempting that, you have a couple of choices:
1) Put the `Window` contents into a `DataTemplate` and then display that content in a `ContentControl` in a `Window`:
<DataTemplate DataType="{x:Type viewmodel:ViewModel}">
<!-- Define content -->
</DataTemplate>
...
<ContentControl Content="{Binding ViewModelProperty}" />
2) Use a `UserControl` instead of a `Window` element:
<DataTemplate DataType="{x:Type viewmodel:ViewModel}">
<UserControl>
<!-- Define Content here -->
</UserControl>
</DataTemplate>
Problem
I have the feeling that I am missing something very important. I've created the following code: File: App.xaml ``` <Application x:Class="HelloWorld.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Startup="Application_Startup"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="MainView.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> ``` File: MainWindow.xaml ``` <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewmodel="clr-namespace:HelloWorld.ViewModel"> <DataTemplate DataType="{x:Type viewmodel:ViewModel}"> <Window Title="HelloWorld"> <Window.Resources> <Style TargetType="TextBlock"> <Setter Property="Margin" Value="50"></Setter> </Style> </Window.Resources> <TextBlock Text="TODO" /> </Window> </DataTemplate> </ResourceDictionary> ``` Although I can compile the code and everything looks good. I get the grey wiggly lines from `<Window Title="HelloWorld">` to `</Window>` with the message "Can't put a Window in a Style.". I am wondering what did I do wrong? What should I do to improve my code? I am btw trying to use the MVVM. Thanks!