設定オブジェクトからいくつかのフィールドをフォーマットしたいリストビューがあります。最初に、空白のページがあり、例外の原因が示されていない、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を使用しているのに、なぜこの例外が発生するのですか?