28

ViewModelを使用してIsSelected値を格納し、をViewModel IsSelectedにバインドすることにより、この投稿で説明されているものと同様の選択パターンを実装しました。ListViewItem.IsSelected

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
    </Style>
</ListView.ItemContainerStyle>

一般的には動作しますが、深刻な問題が発生します。VirtualizingStackPanelリストビューのパネルとしてaを使用すると、表示されているものだけListViewItemが作成されます。「Ctrl+A」を使用してすべてのアイテムを選択するか、最初のアイテムで「Shift + Ctrl + End」などのショートカットの組み合わせを使用すると、すべてのアイテムが選択されますが、非表示のアイテムの場合、ViewModelはIsSelectedを取得しませんtrueに設定します。ListViewItemが作成されていない場合、バインディングが機能しないため、これは論理的です。

誰かが同じ問題を経験し、解決策を見つけました(を使用しないことは別としてVirtualizingStackPanel)?

4

3 に答える 3

32

MVVMパターンで選択を処理する別の方法を見つけました。これにより、問題が解決しました。ビューモデルで選択を維持する代わりに、選択はListView / ListBoxから取得され、パラメーターとしてコマンドに渡されます。すべてXAMLで行われます:

<ListView 
    x:Name="_items"
    ItemsSource="{Binding Items}" ... />

<Button 
    Content="Remove Selected" 
    Command="{Binding RemoveSelectedItemsCommand}" 
    CommandParameter="{Binding ElementName=_items, Path=SelectedItems}"/>

私のViewModelで:

private void RemoveSelection(object parameter)
{
    IList selection = (IList)parameter;
    ...
}
于 2009-08-13T19:43:48.130 に答える
1

Apart from not using VirtualizingStackPanel, the only thing I can think of is to capture those keyboard shortcuts and have methods for modifying a certain range of your ViewModel items so that their IsSelected property is set to True (e.g., SelectAll(), SelectFromCurrentToEnd()). Basically bypassing the Binding on ListViewItem for controlling the selection for such cases.

于 2009-08-13T19:08:13.580 に答える