Search code examples
c#wpfxamllistviewdatatemplate

WPF put ListView items DataTemplate to a separate file


I have a simple task, but a little bit confused with it. So, I have a ListView on some of my windows and I have a custom ListView item that I put in the separate file calls "ListViewItem.xaml" and I've made a custom style for my ListView in "Styles.Constrols.ListView.Xaml". My "MyListViewItem.xaml" looks like this:

<UserControl x:Class="SomeApp.Views.MyListViewItem"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" 
             d:DesignHeight="40" 
             d:DesignWidth="400"
             Margin="0">
    <StackPanel Orientation="Horizontal">
        <Button Height="40" Width="60" Content="{Binding Id}" Foreground="Chartreuse"  />
        <ComboBox Margin="2 0 0 0" ItemsSource={Binding Values}/>
    </StackPanel>
</UserControl>

My "Styles.Constrols.ListView.Xaml" is:

  <Style x:Key="Style.ListViews.MainWindow.CenterPanel" TargetType="{x:Type ListView}">     
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <views:MyListViewItem/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>

At this moment all works fine BUT when I try to change the type of MyListViewItem from UserControl to ListViewItem I'm getting an error at this line

<DataTemplate>
   <views:MyListViewItem/>
</DataTemplate>

property 'visual tree' does not support values of type "ListViewItem"

When I do this little trick:

<DataTemplate>
    <ListViewItem 
     xmlns....>
        <StackPanel Orientation="Horizontal">
            <Button Height="40" Width="60" Content="{Binding Id}" Foreground="Chartreuse"  />
            <ComboBox Margin="2 0 0 0" ItemsSource={Binding Values}/>
        </StackPanel>
    </ListViewItem >
</DataTemplate>

It becomes working fine. What is the point of this behavior?

So my questions are as follows

  • Is it so necessary to use UserControl for all elements that we want to put from window layout (for better readability) into separate .xaml file ?
  • ListViewItem And UserControl are both children of ContentControl, why I can't use ListViewItem when I put its layout in separate file?

Solution

  • The following should work.

    MyListViewItem.xaml:

    <ListViewItem x:Class="SomeApp.Views.MyListViewItem"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="450" d:DesignWidth="800">
        <StackPanel Orientation="Horizontal">
            <Button Height="40" Width="60" Content="{Binding Id}" Foreground="Chartreuse"  />
            <ComboBox Margin="2 0 0 0" ItemsSource="{Binding Values}"/>
        </StackPanel>
    </ListViewItem>
    

    MyListViewItem.xaml.cs:

    namespace SomeApp.Views
    {
        /// <summary>
        /// Interaction logic for MyListViewItem.xaml
        /// </summary>
        public partial class MyListViewItem : ListViewItem
        {
            public MyListViewItem()
            {
                InitializeComponent();
            }
        }
    }
    

    Is it so necessary to use UserControl for all elements that we want to put from window layout (for better readability) into separate .xaml file ?

    No.

    ListViewItem And UserControl are both children of ContentControl, why I can't use ListViewItem when I put its layout in separate file?

    You can.