2012-06-05 30 views
0

我有一個列表視圖,我想從中填充對象的幾個字段。最初,我創建了一個數據模板,它引發了一個空白頁面的InvalidOperationException,並且沒有指出異常的原因。我發現在CodeProject上的文章,我有現在的DataTemplate嵌入在二傳手定義:ListView在加載時返回異常

<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爲什麼我得到這個異常?

+0

它拋出什麼錯誤?請把我們正在尋找的錯誤... – Hituptony

+0

錯誤是主題行。在使用ItemSource之前,Items集合必須爲empty。 –

回答

0

事實證明,在XAML的主線中設置邊框和背景畫筆是個問題。我刪除它們並在我的DataTemplate中使用Setter.Property,現在應用程序運行。

所以對於未來的設計:定義ListView與數據模板的外觀時,重要的是樣式保留在數據模板中。我的最終頁面設計結束了很大的不同,因爲我添加了一些格式如下this blog