2011-07-14 9 views
3

グリッドとボタンがあります。 下向き矢印を押して行を選択し、ボタンをクリックしてから、をクリックして次の行を選択し、ボタンをクリックすることができます。ボタンを左クリックしてもグリッドのフォーカス状態を保持

ただし、ボタンをクリックすると、グリッドのキーボードフォーカスが行に表示されなくなります。

例を示します。この写真では、3番目の行が選択されています。私が望む振る舞いは:を私がクリックしてを押してから、キーボードの下向き矢印を1回押して4番目の行を選択します。代わりに、矢印の下にを押したとき、最上部のセルには枠があり、もう一度押すと最上部の行が選択されます。

A WPF grid and a button

は、どのように私は私が必要な行動を作成に取り掛かる必要がありますか?私は明示的にグリッドに焦点を当てるためにコードを編集しようとしましたが、うまくいきませんでした。

例の完全なコードは次のとおりです。

<Window x:Class="WpfTest.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
<Window.Resources> 
     <XmlDataProvider x:Key="DcCharacters"> 
      <x:XData> 
       <Characters xmlns=""> 
        <Character HeroName="Catwoman" Identity="Selina Kyle" /> 
        <Character HeroName="Batman" Identity="Bruce Wayne" /> 
        <Character HeroName="Starman" Identity="Jack Knight" /> 
        <Character HeroName="BlueBeetle" Identity="Jaime Reyes" /> 
       </Characters> 
      </x:XData> 
     </XmlDataProvider> 
    </Window.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*" /> 
      <RowDefinition Height="Auto" /> 
     </Grid.RowDefinitions> 
     <DataGrid 
      Grid.Row="0" 
      ItemsSource="{Binding Source={StaticResource DcCharacters}, 
      XPath=//Characters/Character}" 
      AutoGenerateColumns="False" 
          IsReadOnly="True" 
      > 
      <DataGrid.Columns> 
       <DataGridTextColumn Binding="{Binding [email protected]}" /> 
       <DataGridTextColumn Binding="{Binding [email protected]}" /> 
      </DataGrid.Columns> 

     </DataGrid> 
     <StackPanel Grid.Row="1"> 
      <Button Margin="3,3,3,3" Width="Auto" Height="Auto" HorizontalAlignment="Left">_Press Me!</Button> 
     </StackPanel> 
    </Grid> 
</Window> 

答えて

2

私はあなたが添付プロパティFocusManager.IsFocusScopeを探していると思います。

StackPanelでtrueに設定すると、マウスでクリックすると、その中のButton's(または他のコントロール)がフォーカスを奪うことはありません。ツールバーボタンやメニューのように機能します。あなたはまだそれらにタブすることができるでしょう。あなたの代わりに1、カット、コピーの3つのボタンがあり、

<StackPanel Grid.Row="1" 
      FocusManager.IsFocusScope="True" > 
    <Button Width="Auto" Height="Auto" HorizontalAlignment="Left">Cut</Button> 
    <Button Width="Auto" Height="Auto" HorizontalAlignment="Left">Copy</Button> 
    <Button Width="Auto" Height="Auto" HorizontalAlignment="Left">Paste</Button> 
</StackPanel> 

またはあなたの場合には、ハックのちょうど

<StackPanel Grid.Row="1" 
      FocusManager.IsFocusScope="True"> 
    <Button Margin="3,3,3,3" Width="Auto" Height="Auto" HorizontalAlignment="Left">_Press Me!</Button> 
</StackPanel> 
1

あなたのボタン用にちょうどFocusable = "False"わたしにはできる。

<Button Margin="3,3,3,3" Width="Auto" Height="Auto" HorizontalAlignment="Left" Focusable="False">_Press Me!</Button> 
+0

+1です。私の唯一の問題は、私が望むなら、私はそのボタンにタブすることができないということです。 (私達がすべてを達成できるかどうかを見てみましょう:-) –

+0

ただ一つのことが私の心に浮かぶ。あなたがテーブルの中にいるとき - それは間違っている。あなたが離れたとき - その真実。 – stukselbax

1

種類を貼り付けた場合ここで

は一例ですが、それは

の作品
private void Window_PreviewKeyDown(object sender, KeyEventArgs e) 
     { 
      if (myDataGrid.SelectedItem != null && e.Key == Key.Down) 
      { 
       DataGridRow dgrow = (DataGridRow)myDataGrid.ItemContainerGenerator.ContainerFromItem(myDataGrid.SelectedItem); 
       dgrow.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next)); 
      } 
     } 
+0

+1ニース。私はボタンのクリックハンドラを無効にしようとしていましたが、トンネリングイベントを通して矢印キーを操作する方がずっと賢いです。 –

関連する問題