ss7
ss7

Reputation: 3012

XAML WPF Data Templates

I have this XAML code:

<Window x:Class="WpfApplication6.ApplicationView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfApplication6"
    Title="Simple MVVM Example" Height="350" Width="525">

<Window.Resources>
    <DataTemplate DataType="{x:Type local:ApplicationViewModel}">
        <local:HomeView />
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:ProductViewModel}">
        <local:ProductsView />
    </DataTemplate>
</Window.Resources>

<DockPanel>
    <Border DockPanel.Dock="Left" BorderBrush="Black" BorderThickness="0,0,1,0">
        <ItemsControl ItemsSource="{Binding PageViewModels}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Content="{Binding Name}"
                            Command="{Binding DataContext.ChangePageCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                            CommandParameter="{Binding }"
                            Margin="2,5"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>
    </Border>

    <ContentControl Content="{Binding CurrentPageViewModel}" />
</DockPanel>
</Window>

The part in Window.Resources is giving an error: Can't put a Window in a Style. Both of the views throw the error, HomeView and ProductsView. I have some questions:

Upvotes: 1

Views: 43

Answers (1)

Daniel Rose
Daniel Rose

Reputation: 17638

I'm guessing that HomeView and ProductsView are of type Window, which is the error you are getting. Instead, they need to be a UserControl or similar type.

Upvotes: 2

Related Questions