New user, List View

1 Answer 192 Views
ListView
Jonathan
Top achievements
Rank 1
Iron
Jonathan asked on 13 Apr 2022, 01:32 PM

Ok so I have started a new project from the template and this is my XAML code#

I am getting the following error and cannot work out why ?

WMC0035 Duplication assignment to the 'Content' property of the 'Window'

WMC9997 There are multiple root elements. Line 21, position
<Window
    x:Class="Megasync_EAB.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ListViewBasicSample">
    <Grid>
        <ListView
        x:Name="BaseExample"
        ItemTemplate="{StaticResource ContactListViewTemplate}"
        BorderThickness="1"
        BorderBrush="{ThemeResource SystemControlForegroundBaseMediumLowBrush}"
        Width="350"
        Height="400"
        HorizontalAlignment="Left"/>
    </Grid>

</Window>

<DataTemplate x:Key="ContactListViewTemplate" x:DataType="local:MegaData">
    <TextBlock Text="{x:Bind Name}" x:Phase="1" Margin="0,5,0,5"/>
</DataTemplate>


1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 13 Apr 2022, 02:05 PM

Hi Jonathan,

Thank you for sharing the code, without it I wouldn't be able to tell you exactly what to modify.

The problem is the DataTemplate, it needs to go into a Resources section (in XAML all keyed items go into Resources). Most commonly it is put in App.xaml's Resources, but in this case, you can use it in the Grid.Resources instead:

<Window x:Class="Megasync_EAB.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ListViewBasicSample">
    <Grid>
        <Grid.Resources>
            <DataTemplate x:Key="ContactListViewTemplate" x:DataType="local:MegaData">
                <TextBlock Text="{x:Bind Name}" x:Phase="1" Margin="0,5,0,5"/>
            </DataTemplate>
        </Grid.Resources>
        
        <ListView x:Name="BaseExample"
                  ItemTemplate="{StaticResource ContactListViewTemplate}"
                  BorderThickness="1"
                  BorderBrush="{ThemeResource SystemControlForegroundBaseMediumLowBrush}"
                  Width="350"
                  Height="400"
                  HorizontalAlignment="Left"/>
    </Grid>
</Window>

Future Assistance

I do not see any Telerik controls in this code. That is the Microsoft ListView and not the Telerik RadListView.

The Telerik forums are helping with for Telerik UI for WinUI controls features. A non-relevant question here may or may not get answered by the community, so I wanted to clear this up so that you can always get the fastest, most relevant help.

If you need help with Telerik controls, you can choose a option in the Telerik forums. For all other XAML/WinUI/UWP questions, I recommend opening a post in one of these locations:

Choosing one of those locations will ensure you get an answer in a timely manner for the most relevant topic.

Regards,
Lance | Manager Technical Support
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Jonathan
Top achievements
Rank 1
Iron
commented on 13 Apr 2022, 02:09 PM

Thank you so much for your help. I am looking to add Telerik once I have basics up and running. Thanks for the resources.
Lance | Manager Technical Support
Telerik team
commented on 13 Apr 2022, 03:51 PM

I'm happy to see this was helpful!

Before I close this, I also wanted to clarify why you would put that DataTemplate in the Grid.Resources versus putting it in the App.xaml Resources.

I put it in the Grid.Resources to keep my reply's code short to the point, but you should be aware that if you have a ListView using that ItemTemplate on another page (or anywhere outside of that Grid), you would get an exception saying that "ContactListViewTemplate is not available" on that other ListView.

This is because Resources are scoped to only the children of where you put it. Meaning only the children of that Grid can use resources defined in the Grid.Resources section.

If you put it in the App.xaml Resources, that DataTemplate is now available to the entire app:

<Application ...>
  <Application.Resources>
        ...
            <DataTemplate x:Key="ContactListViewTemplate" x:DataType="local:MegaData">
                <TextBlock Text="{x:Bind Name}" x:Phase="1" Margin="0,5,0,5"/>
            </DataTemplate>
    </Application.Resources>

</Application>

 

Tags
ListView
Asked by
Jonathan
Top achievements
Rank 1
Iron
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or