2012-03-23 66 views
1

我正在重温一个我曾经工作过的MVVM Windows Phone项目,当我将一个Trigger添加到ListBox时遇到问题。我已将项目升级到7.1。我以前有代码如下: -设置针对ListBox的触发器时引发的异常

  <ListBox x:Name="MainListBox" 
       ItemsSource="{Binding MyItems}" 
       Grid.Row="1" 
       Margin="12" 
       ItemContainerStyle="{StaticResource MyListItemStyle1}" 
       SelectedIndex="{Binding CurrentSelectedIndex, Mode=TwoWay}" 
       ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
       i:Interaction.Triggers="{StaticResource PerformSelectionChangedEventTrigger}" /> 

不过我现在收到的异常,如下所示: -

无法设置只读属性“System.Windows.Interactivity.Interaction.Triggers

进行以下更改似乎解决这个问题: - 我已经寻找异常消息

  <ListBox x:Name="MainListBox" 
       ItemsSource="{Binding MyListItems}" 
       Grid.Row="1" 
       Margin="12" 
       ItemContainerStyle="{StaticResource MyListItemStyle1}" 
       SelectedIndex="{Binding CurrentSelectedIndex, Mode=TwoWay}" 
       ScrollViewer.HorizontalScrollBarVisibility="Disabled">     
      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="SelectionChanged"> 
        <cmdextras:EventToCommand Command="{Binding Path=PerformSelectionChanged}" 
               PassEventArgsToCommand="True" /> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 
     </ListBox> 

但是我不确定为什么“内联”触发设置使用为w ork不再有效。

有人能够对此有所了解吗?

回答

2

我会说,当你这样做内嵌设置的值。你正在设置一个集合。

通常建议将集合只读,他们可能会改变这一点,并以另一种方式做,添加到集合中,即使集合是只读集合也没有问题。