我有一个列表视图,我想从一个填充对象中格式化几个字段。最初,我创建了一个数据模板,它抛出了一个带有空白页的 InvalidOperationException,并且没有指示异常的原因。我在 CodeProject 上找到了一篇文章,现在我将数据模板嵌入到了 Setter 定义中:
<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>
通过定义 ASIS,我现在得到一个运行时异常,它最终指向 XAML。但我仍然不明白为什么我得到它。例外:
'向'System.Windows.Controls.ItemCollection'类型的集合添加值引发了异常。' 行号“55”和行位置“13”。
它有内部异常:
{“使用 ItemsSource 时操作无效。请改用 ItemsControl.ItemsSource 访问和修改元素。”}
我显然在使用 ItemsSource 那么为什么会出现此异常?