ListView在加载时返回异常

时间:2012-06-05 14:43:41

标签: c# xaml exception

我有一个listview,我想从填充对象格式化几个字段。最初,我创建了一个数据模板,该模板使用空白页面抛出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,为什么我会得到这个异常?

1 个答案:

答案 0 :(得分:0)

事实证明,在XAML主线中设置边框和背景画笔是个问题。我删除了它们并在我的DataTemplate中使用Setter.Property,现在应用程序运行。

因此,对于未来的设计:在使用数据模板定义ListView的外观时,将样式保留在数据模板中非常重要。由于我在this blog

之后添加了一些格式,我的最终页面设计结果明显不同