2017-06-20 15 views
0

私はリストが表示されているブック付きです。 SelectedIndexCurrentindexは、ViewmodelのPropertiesにバインドされています。 ListBoxの下にはViewModelのSelectedIndexプロパティからの単純な追加(次)または削除(最後)の2つのボタンがあります。 NextLastボタンを押す選択したインデックスを変更すると、選択したアイテムをリストボックスに表示します。スクロールListBox

<DockPanel LastChildFill="False"> 
    <ListBox DockPanel.Dock="Top" 
      ItemsSource="{Binding Books, Mode=OneWay}" 
      SelectedItem="{Binding CurrentBook, Mode=TwoWay}" 
      SelectedIndex="{Binding SelectedIndex}" 
      /> 
    <StackPanel Orientation="Horizontal" DockPanel.Dock="Bottom"> 
     <Button Content="Last" Command="{Binding ChangeBook}" CommandParameter="last"/> 
     <Button Content="Next" Command="{Binding ChangeBook}" CommandParameter="next"/> 
    </StackPanel> 
</DockPanel> 

は、リストボックスのインデックスを変更します。何も起こらないのは、ユーザーが目に見える範囲から索引を外すことができない場合、ListBoxはスクロールせず、選択した索引を表示したままにすることです。

ユーザーがスクロールしない限り、SelectedItemは常にListBoxに表示されます。
私はこれをやりたいのですが、dooingしながらMVVMパターンを破ることはしません。

答えて

1

MVVMに違反することなくこれを行う方法は次のとおりです。背後

<ListBox DockPanel.Dock="Top" 
     ItemsSource="{Binding Books, Mode=OneWay}" 
     SelectedItem="{Binding CurrentBook, Mode=TwoWay}" 
     SelectedIndex="{Binding SelectedIndex}" 
     SelectionChanged="ListBox_SelectionChanged" 
     /> 

コード:

private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    var lb = (ListBox)sender; 

    lb.ScrollIntoView(lb.SelectedItem); 
} 

そして、ここではそれを行うには、より立派な方法です。何もないこの種のコードバックを使用していますが、このような動作が複数のビューで複数回使用される場合は、イベントハンドラをコピーして貼り付けるよりも、添付プロパティを使用する方が便利です。また、DataTemplateのような文脈でも使用できるようになりました。ここにはコードがありません。

コードが少し前ですが、添付されたプロパティをプロジェクトのListBoxに簡単に追加できます。

public static class ListBoxExtensions 
{ 
    #region ListBoxExtensions.KeepSelectedItemVisible Attached Property 
    public static bool GetKeepSelectedItemVisible(ListBox lb) 
    { 
     return (bool)lb.GetValue(KeepSelectedItemVisibleProperty); 
    } 

    public static void SetKeepSelectedItemVisible(ListBox lb, bool value) 
    { 
     lb.SetValue(KeepSelectedItemVisibleProperty, value); 
    } 

    public static readonly DependencyProperty KeepSelectedItemVisibleProperty = 
     DependencyProperty.RegisterAttached("KeepSelectedItemVisible", typeof(bool), typeof(ListBoxExtensions), 
      new PropertyMetadata(false, KeepSelectedItemVisible_PropertyChanged)); 

    private static void KeepSelectedItemVisible_PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
    { 
     var lb = (ListBox)d; 

     if ((bool)e.NewValue) 
     { 
      lb.SelectionChanged += ListBox_SelectionChanged; 
      ScrollSelectedItemIntoView(lb); 
     } 
     else 
     { 
      lb.SelectionChanged -= ListBox_SelectionChanged; 
     } 
    } 

    private static void ScrollSelectedItemIntoView(ListBox lb) 
    { 
     lb.ScrollIntoView(lb.SelectedItem); 
    } 

    private static void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     ScrollSelectedItemIntoView((ListBox)sender); 
    } 
    #endregion ListBoxExtensions.KeepSelectedItemVisible Attached Property 
} 

XAML:

<ListBox DockPanel.Dock="Top" 
     ItemsSource="{Binding Books, Mode=OneWay}" 
     SelectedItem="{Binding CurrentBook, Mode=TwoWay}" 
     SelectedIndex="{Binding SelectedIndex}" 
     local:ListBoxExtensions.KeepSelectedItemVisible="True" 
     /> 
+0

迅速な応答のためのおかげで、それは明日を見ていきます^^ –

関連する問題