ListBox SelectionChanged EventTrigger抛出XamlParseException

时间:2014-05-15 17:52:51

标签: wpf listbox mvvm-light xamlparseexception

我想在MVVM Light的SelectionChanged触发器的帮助下实现EventToCommand事件,但是当页面加载时我有异常(如果我评论i:EventTrigge r部分来自xaml,页面显示正常。)

这是ListBox xaml定义

      <ListBox x:Name="lstTopicList" ItemsSource="{Binding TopicList}" 
        SelectionMode="Extended"
              HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" SelectedIndex="0" Height="200" Margin="0,10,0,0" >
        <i:EventTrigger EventName="SelectionChanged">
          <mvvm:EventToCommand
            Command="{Binding TopicSelectionChangedCommand, Mode=OneWay}"
            EventArgsConverter="{StaticResource SelectionChangedEventArgsToListOfObjectConverter}"
            EventArgsConverterParameter="{Binding ElementName=lstTopicList}"
            PassEventArgsToCommand="True" />
        </i:EventTrigger>

        <ListBox.ItemTemplate>
          <DataTemplate>
            <StackPanel>
              <TextBlock Text="{Binding TopicName}" FontWeight="Bold" />
              <TextBlock Text="{Binding TopicDescription}" FontStyle="Italic" Margin="5,0,0,0" />
            </StackPanel>
          </DataTemplate>
        </ListBox.ItemTemplate>
      </ListBox>

这是一个例外:

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  Message='Add value to collection of type 'System.Windows.Controls.ItemCollection' threw an exception.' Line number '73' and line position '17'.
  Source=PresentationFramework
  LineNumber=73
  LinePosition=17
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at JolTudomE_Client.View.StudentView.InitializeComponent() in c:\Users\ihorvath\Documents\Visual Studio 2012\Projects\JolTudomE_WS\JolTudomE_Client\View\StudentView.xaml:line 1
       at JolTudomE_Client.View.StudentView..ctor() in c:\Users\ihorvath\Documents\Visual Studio 2012\Projects\JolTudomE_WS\JolTudomE_Client\View\StudentView.xaml.cs:line 21
  InnerException: System.InvalidOperationException
       HResult=-2146233079
       Message=Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
       Source=PresentationFramework
       StackTrace:
            at System.Windows.Controls.ItemCollection.Add(Object newItem)
            at MS.Internal.Xaml.Runtime.ClrObjectRuntime.Add(Object collection, XamlType collectionType, Object value, XamlType valueXamlType)
       InnerException: 

我的实施可能会出现什么问题?

1 个答案:

答案 0 :(得分:2)

您应该添加Interaction.Triggers代码。

试试这个:

<ListBox x:Name="lstTopicList" ItemsSource="{Binding TopicList}" 
  SelectionMode="Extended"
        HorizontalAlignment="Left" VerticalAlignment="Top" Width="200" SelectedIndex="0" Height="200" Margin="0,10,0,0" >
  <i:Interaction.Triggers>
      <i:EventTrigger EventName="SelectionChanged">
        <mvvm:EventToCommand
          Command="{Binding TopicSelectionChangedCommand, Mode=OneWay}"
          EventArgsConverter="{StaticResource SelectionChangedEventArgsToListOfObjectConverter}"
          EventArgsConverterParameter="{Binding ElementName=lstTopicList}"
          PassEventArgsToCommand="True" />
      </i:EventTrigger>
   </i:Interaction.Triggers>

  <ListBox.ItemTemplate>
    <DataTemplate>
      <StackPanel>
        <TextBlock Text="{Binding TopicName}" FontWeight="Bold" />
        <TextBlock Text="{Binding TopicDescription}" FontStyle="Italic" Margin="5,0,0,0" />
      </StackPanel>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>