私はIsSelectedにバインドすることで、MVVMデザインのListView選択の変更を追跡しています。 IsSynchronizedWithCurrentItemを有効にすることによって現在のアイテムを追跡する必要もあります。MVVM:リストへのバインドIsSynchronizedWithCurrentItemのトラッキング中にIsSelected
は、私は私は2つのListViewコントロールが同じコレクションに結合していたとき、私はInvalidOperationExceptionがを得ることを見つける:「コレクションが変更された。列挙操作が実行されない可能性は、」2つのリストビューの間synchonizationエラーのようです。 1つはPropertyChangedイベントをトリガーし、もう1つはSelectorをおそらく更新していますか?
私はIsSynchronizedWithCurrentItemの使用を控え、それを自分で管理する以外に、この問題を回避する方法を理解できません。何か案は?
ありがとうございました。
のViewModelと背後にあるコード:私はあなたの問題の直接の修正を提供することはできません
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="100" Width="100">
<StackPanel>
<ListView DataContext="{Binding Items}" ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True" SelectionMode="Single">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name, Mode=OneWay}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView DataContext="{Binding Items}" ItemsSource="{Binding}"
IsSynchronizedWithCurrentItem="True" SelectionMode="Single">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name, Mode=OneWay}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackPanel>
</Window>
はい、実際これは私が現在問題を回避する方法です。それを追加すると、コレクションの変更に伴いPropertyChangedの登録を追加/削除するアイテムのCollectionChangedイベントを監視する必要があります。 – Terrence
@terrenceもしこれが働いていれば、昨年より良い方法を見つけることができたら、彼の答えを受け入れるべきですか? –