2014-06-16 7 views
0

내 listbox/wrappanel 화살표 탐색과 관련하여 이상한 문제가 있습니다.ListBox with WrapPanel - 항목/화살표 키 탐색 내리기

내 목록 상자 :

<ListBox x:Name="List" 
    IsSynchronizedWithCurrentItem="True" 
    SelectedIndex="{Binding MainIndex, Mode=OneWayToSource}" 
    SelectedItem="{Binding CurrentFeed, Mode=TwoWay}" 
    ItemsSource="{Binding CurrentFeedList}" 
    ScrollViewer.HorizontalScrollBarVisibility="Disabled" > 
<ListBox.ItemsPanel> 
    <ItemsPanelTemplate> 
     <WrapPanel IsItemsHost="True" MaxWidth="{Binding ActualWidth, ElementName=Panel}" /> 
    </ItemsPanelTemplate> 
</ListBox.ItemsPanel> 
<ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel Width="168"> 
      <Border BorderBrush="White" BorderThickness="0" Margin="0,7,0,0"> 
       <Image Source="{Binding Image , Converter={StaticResource ByteArraytoImageSource}}" Width="150" Height="213" ></Image> 
      </Border> 
      <Border BorderBrush="White" BorderThickness="0" Height="65" HorizontalAlignment="Center" > 
       <TextBlock VerticalAlignment="Center" TextWrapping="Wrap" FontFamily="{StaticResource DefaultFontFamily}" FontSize="15" Text="{Binding Title}" Foreground="White" Cursor="Hand" HorizontalAlignment="Center" TextAlignment="Center"/> 
      </Border> 
     </StackPanel> 
    </DataTemplate> 
</ListBox.ItemTemplate> 

가 렌더 다음 렌더링 Render of Listbox http://muby53.free.fr/render.png

아무 문제 좋은입니다! ;)

RSS에서 검색 한 배경 화면 목록입니다.

나는 하나 개의 항목이 읽혀질 수 있도록 바로 가기를 바인딩 한 후이 ObservableCollection에 제거 것 : 내보기 모델에서

<UserControl.InputBindings> 
    <KeyBinding Key="D" Modifiers="Control" Command="{Binding ReadCmd}" /> 
</UserControl.InputBindings> 

는 :

ObservableCollection<Feed> CurrentFeedList { get; set; } 
private Feed _currentFeed; 
public Feed CurrentFeed 
{ 
    get { return _currentFeed; } 
    set 
    { 
     _currentFeed = value; 
     OnPropertyChanged("CurrentFeed"); 
    } 
} 
public ICommand ReadCmd { get; set; } 
public int MainIndex { get; set; } 

내 ReadCmd는 "ReadAction 부르는 RelayCommand입니다 "방법.

처음에는 단순히 ObservableCollection에서 항목을 제거하지만 Ctrl + D를 두 번 눌러 2 항목을 읽으려고합니다.

그래서 목록 상자의 색인을 가져와 동일한 색인을 제거한 다음 다시 선택하기로 결정했습니다.

private void ReadAction() 
{ 
    int previousIndex = MainIndex; 

    if (previousIndex == CurrentFeedList.Count - 1) 
     previousIndex -= 1; 

    CurrentFeedList.Remove(CurrentFeed); 

    CurrentFeed = CurrentFeedList[previousIndex]; 
} 

내가 원하는대로 작동하지만 한 가지 문제가 남아 있으며 해결할 수 없습니다.

Ctrl + D를 누르면 SelectedItem이 제거되고 다음 항목이 Selected로 바뀝니다. 그러나 화살표 키 탐색을 사용하여 목록을 탐색하면 매번 목록의 첫 번째 항목으로 이동합니다.

충분히 명확하길 바랍니다.

감사합니다.

답변

0

이 문제로 인해 당신이 KeyBinding를 사용하여 항목을 제거 할 때, ListBox은 초점을 잃고 장소가 ListBox의 첫 번째 항목에 초점을 그 후 따라서 화살표 키를 눌러 있다는 사실이다.

따라서 선택한 항목에서 화살표 키를 작동 시키려면 Listbox의 선택된 항목에 초점을 설정해야합니다.

두 가지 방법이 있습니다. 예를 들어 첨부 된 동작을 사용하여 ListBoxItem에 포커스를 설정합니다. 간단한은 목록 상자와 처리기에서 SelectionChanged 이벤트가 아래와 같이 현재 선택된 항목에 포커스를 설정 캡처하는 것입니다 : 당신이 최고야

private void MyListBox_OnSelectionChanged(object sender, RoutedEventArgs e) 
    { 
     var item = (ListBoxItem)MyListBox.ItemContainerGenerator.ContainerFromItem(MyListBox.SelectedItem); 

     if(item !=null) 
      item.Focus(); 
    } 
+0

! 짧고 아주 좋은 대답! 감사합니다 ! – Muby