質問

I want to implement SelectionChanged event with the help of EventToCommand trigger of MVVM Light, but I've got that exception, when the page is loading (if i comment the i:EventTrigger part from xaml the display of the page is working).

This is the ListBox xaml definition

      <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>

This is the exception:

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: 

what could be the problem with my implementation?

役に立ちましたか?

解決

You should add Interaction.Triggers tag.

Try this:

<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>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top