1

WPF コントロールにいくつかの画像 (位置) があります。ListView各画像は、そのコントロールの下にある項目に関連付けられています。ユーザーが位置をクリックすると、対応する位置ListViewItemが選択されます (したがって、強調表示されます) ListView。同様に、ユーザーが をクリックするListViewItemと、対応する位置が選択されるようにします。

どちらか一方の動作を行うことはできますが、両方を一緒に機能させることはできないようです。

位置が選択されたときにプロパティを「true」にStyle設定する があります。IsSelected

<Style x:Key="PositionItem" TargetType="ListViewItem">
    <Setter Property="IsSelected" Value="False" />
    <Style.Triggers>
        <DataTrigger Value="True">
            <DataTrigger.Binding>
                <MultiBinding Converter="{StaticResource IsCurrentPositionConverter}">
                    <Binding RelativeSource="{RelativeSource Self}" Path="DataContext" />
                    <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type UserControl}}" Path="DataContext.CurrentBackplane.CurrentCard.CurrentPosition" />
                </MultiBinding>
            </DataTrigger.Binding>
            <Setter Property="IsSelected" Value="True" />
        </DataTrigger>
    </Style.Triggers>
</Style>

私のListViewでは、次のハンドラーを設定しましたSelectionChanged

private void Positions_SelectionChanged(object sender, SelectionChangedEventArgs e) {
    var listView = sender as ListView;
    if (listView == null) return;
    var currentPos = listView.SelectedItem as IGraphicPositionViewModel;
    if (currentPos == null) return;
    if (currentPos != _ViewModel.CurrentBackplane.CurrentCard.CurrentPosition)
        _ViewModel.CurrentBackplane.CurrentCard.CurrentPosition = currentPos;
}

問題は、 のIsSelectedプロパティがのプロパティListViewItemとうまく相関していないように見えることです。SelectedItemListView

これらのプロパティを同期するために使用できる他のプロパティまたはイベントはありますか?

4

1 に答える 1

0

SelectedPositionプロパティとを使用してビューモデルを定義する必要がありますSelectedListItem。次に、これらのプロパティの1つのイベントハンドラーで、別のプロパティを変更する必要があります。

    private Position _selectedPosition;
    public Position SelectedPosition
    {
        get
        {
            return _selectedPosition;
        }
        set
        {
            if (_selectedPosition != value)
            {
                _selectedPosition = value;
                RaisePropertyChanged("SelectedPosition");
                _OnSelectedPositionChanged();
            }
        }
     }

     private void _OnSelectedPositionChanged()
     {
         _selectedListItem = ... ; // find corresponding item in the list
         RaisePropertyChanged("SelectedListItem"); // update selection of the ListView
     }

また、同様のコードをSelectedListItemイベントハンドラーで使用する必要があります。

于 2012-06-22T03:27:06.900 に答える