2014-03-05 133 views
3

我輸出的東西類似於Windows 8的開始菜單。將MoveFocus從一個列表框移動到另一個列表框

這裏是我的輸出的屏幕截圖:

enter image description here

我已經成功地得到了輸出由this問題得到幫助。

XAML實現以下的輸出:

<ItemsControl ItemsSource="{Binding MenuCategories}" > 

    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel IsItemsHost="True" Orientation="Horizontal" /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 

    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Grid > 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
        <RowDefinition Height="*" /> 
       </Grid.RowDefinitions> 

       <TextBlock Text="{Binding Title}" FontSize="30" /> 

       <ListBox Grid.Row="1" x:Name="lst" ItemsSource="{Binding Design_Master_TileItem}" BorderThickness="0" 
         SelectedItem="{Binding DataContext.SelectedTile, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Page}}}" 
         helpers:SingleSelectionGroup.SelectionGroup="Group"> 

        <ListBox.ItemsPanel> 
         <ItemsPanelTemplate> 
          <WrapPanel IsItemsHost="True" Orientation="Vertical" MaxHeight="{Binding ElementName=lst, Path=ActualHeight}"/> 
         </ItemsPanelTemplate> 
        </ListBox.ItemsPanel> 

        <ListBox.Resources> 
         <Style TargetType="{x:Type ListBoxItem}"> 
          <Setter Property="Width" Value="250" /> 
          <Setter Property="Height" Value="125" /> 
          <Setter Property="Margin" Value="2.5" /> 
          <Setter Property="Padding" Value="2.5" /> 
          <Setter Property="Background" Value="{Binding Background, Converter={StaticResource stringToBrushConverter}}" /> 
          <Setter Property="Foreground" Value="White" /> 
          <Setter Property="VerticalContentAlignment" Value="Bottom" /> 
          <Style.Triggers> 
           <Trigger Property="IsSelected" Value="True"> 
            <Setter Property="Foreground" Value="{Binding Background, Converter ={StaticResource stringToBrushConverter}}" /> 
           </Trigger> 
           <Trigger Property="IsKeyboardFocusWithin" Value="True"> 
            <Setter Property="IsSelected" Value="True"></Setter> 
           </Trigger> 
          </Style.Triggers> 
         </Style> 
        </ListBox.Resources> 

        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Horizontal" Height="125" Width="250"> 
           <Path Data="{Binding ImageData}" VerticalAlignment="Center" 
             Stretch="Uniform" Fill="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}" 
             Width="68" Height="68" Margin="10" RenderTransformOrigin="0.5,0.5"> 
            <Path.RenderTransform> 
             <TransformGroup> 
              <TransformGroup.Children> 
               <RotateTransform Angle="0" /> 
               <ScaleTransform ScaleX="1" ScaleY="1" /> 
              </TransformGroup.Children> 
             </TransformGroup> 
            </Path.RenderTransform> 
           </Path> 
           <TextBlock Text="{Binding Title, Converter={StaticResource spaceToNewLineConverter}}" VerticalAlignment="Top" 
              Margin="40,10,10,10" FontSize="24" Foreground="{Binding Path=Foreground, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"/> 
          </StackPanel> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 

       </ListBox> 
      </Grid> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

所以,從上面的代碼中你可能已經明白,集團和總帳是ListBox1中的兩個項目。其他四個是listbox2的項目。

要求:

假設,ItemA1在listboxA選擇。

案例1:新行爲

如果ListBoxA不會對項目1右側的任何項目,然後當我按下右箭頭鍵重點應轉移到listboxB的ItemB1。同樣,如果選擇listboxA的ItemA2,則焦點應移至ListBoxB的ItemB2。

案例2:默認行爲

如果ListBoxA對ItemA1的右手邊的一些項目,則該項目應該按右箭頭鍵選擇。我默認有這種行爲,但我不想打擾它。我的意思是在實施Case1時,我不想失去默認行爲。

+0

不幸的是,我不能說完全是,但嘗試這種風格添加到'ListBox':'<樣式的TargetType = 「{x:類型列表框}」><觸發屬性=」 HasItems「Value =」False「>'。這意味着如果'ListBox'中沒有元素,他不能接收焦點。 –

+0

不,你完全錯誤地理解了我的問題。 ListBox的ItemsPanel被設置爲WrapPanel,方向=垂直。現在閱讀有關的要求部分。我想你現在會知道我想要什麼。 – Vishal

+0

對不起,沒有再次理解這個問題,但我提前警告說,我不能肯定地說。我認爲這很難做到,因爲你在DataTemplate中有一個'ListBox',並且正在訪問多個'ListBox'。是的,這可以通過一個龐大而醜陋的代碼和算法來完成,這個算法應該考慮到每一方的元素。因此,如果你有另一種選擇,我建議找她。如果沒有,我會接受這個挑戰,我不能確切地說它什麼時候會完成。然而,如果你想讓我開始完成這個任務,請給我一個鏈接到你的項目。 –

回答

2

您當前的XAML代碼工作正常,在這裏..請詢問KeyBoardNavigationKeyboardnavigation

此鏈接,您只需添加KeyboardNavigation.DirectionalNavigation = 「繼續」KeyboardNavigation.TabNavigation =「繼續」到列表框,它按預期工作。

<ItemsControl KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" Focusable="False" > 
     ... 

     <ListBox Grid.Row="1" x:Name="lst" KeyboardNavigation.DirectionalNavigation="Continue" KeyboardNavigation.TabNavigation="Continue" BorderThickness="0"/> 
    .. 

    </ItemsControl> 
+0

謝謝Heena。根據stackoverfow.com的規則,我會在17個小時後給予您賞金。傑伊馬哈拉施特拉。 – Vishal

+0

歡迎您.Jay Maharashtra !! –

2

將焦點從一個ListBox移動到另一個ListBox中的各個項目內需要困難的邏輯。相反,您可以使用單個ListBox並根據Header進行分組。 GroupStyle面板可以更改爲水平方向的堆疊面板來實現您的UI。我想這樣的,按預期工作重點,

<ListBox ItemsSource="{Binding Source={StaticResource source}}" DisplayMemberPath="Name"> 
     <ListBox.GroupStyle> 
      <GroupStyle> 
       <GroupStyle.HeaderTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Name}"/> 
        </DataTemplate> 
       </GroupStyle.HeaderTemplate> 
       <GroupStyle.Panel> 
        <ItemsPanelTemplate> 
         <StackPanel Orientation="Horizontal" /> 
        </ItemsPanelTemplate> 
       </GroupStyle.Panel> 
      </GroupStyle> 
     </ListBox.GroupStyle> 
    </ListBox> 
+0

你可以發佈樣本嗎?因爲我的輸出中有一個空白的列表框。 – Vishal

+1

請嘗試此示例。 https://db.tt/UnSN74Oe [接受答案,如果它可以幫助你] – Jawahar

+0

我對你的示例項目做了一些改變。我上傳了它:https://drive.google.com/file/d/0B5WyqSALui0bXzdtWFVMYnpEWXM/edit?usp=sharing但我在這裏遇到了一個小問題。如何減少左右兩組相同組之間的空間?另外我想增加兩個不同組之間的空間。 – Vishal

相關問題