I have a listview for which I want to format several fields from a populating object. Initially I created a data template which threw an InvalidOperationException with a blank page and no indication of the cause of the exception. I found an article on CodeProject and I have the datatemplate now embedded in a Setter definition:
<UserControl x:Class="Servpro.Framework.ViewerModule.Views.MenuView"
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="580" d:DesignWidth="210">
<UserControl.Resources>
<Style TargetType="ListView">
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Vertical">
<TextBlock
Background="Transparent"
Foreground="Black"
FontSize="12"
Text="{Binding Path=CurrentEvent.EventTypeName, Mode=OneWay}" />
<TextBlock
Background="Transparent"
Foreground="Black"
FontSize="12"
Text="{Binding Path=CurrentEvent.EventMessage, Mode=OneWay}" />
<StackPanel Orientation="Horizontal">
<TextBlock
Background="Transparent"
Foreground="Black"
FontSize="8"
Text="{Binding Path=CurrentEvent.EventLoggedOn, Mode=OneWay}"
Margin="0,0,10,0" />
<TextBlock
Background="Transparent"
Foreground="Black"
FontSize="8"
Text="{Binding Path=CurrentEvent.Program, Mode=OneWay}" />
<TextBlock
Background="Transparent"
Foreground="Black"
FontSize="8"
Text=":" />
<TextBlock
Background="Transparent"
Foreground="Black"
FontSize="8"
Text="{Binding Path=CurrentEvent.Method, Mode=OneWay}" />
</StackPanel>
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid Margin="4">
<ListView
ItemsSource="{Binding Path=EventList, Mode=OneWay}"
Height="568" VerticalAlignment="Top"
Width="201" HorizontalAlignment="Left"
Margin="4" >
<Border CornerRadius="11" />
<ListView.BorderBrush >
<SolidColorBrush Color="#99FFFFFF" Opacity="0" />
</ListView.BorderBrush>
<ListView.Background>
<SolidColorBrush Color="#99FFFFFF" Opacity="0"/>
</ListView.Background>
</ListView>
</Grid>
With the definition ASIS I now get a runtime exception and it is finally pointing at the XAML. But I still do not understand why I’m getting it. The exception:
‘Add value to collection of type ‘System.Windows.Controls.ItemCollection’ threw an exception.’ Line number ’55’ and line position ’13’.
It has the inner exception:
{“Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.”}
I’m clearly using ItemsSource so why am I getting this exception??
It turned out that the setting of the border and background brushes in the main line of the XAML was the issue. I removed them and use Setter.Property in my DataTemplate and the application runs now.
So for future designs: when defining the look of a ListView with a data template, it is important that styling remain with the data template. My final page design ended up to be significantly different as I added some formatting following this blog