2013-06-14 26 views
6

Tôi có một DataGrid trong đó SelectedItem bị ràng buộc với thuộc tính VM Selected. Tôi có một điều khiển tìm kiếm sẽ thực hiện tìm kiếm và SelectedItem của các thay đổi DataGrid (và cuộn vào chế độ xem). WPF 4.0 và DataGridSelectionUnit="FullRow".Tập trung vào DataGridCell cho SelectedItem khi DataGrid nhận tiêu điểm bàn phím

Vấn đề của tôi là tập trung. Các DataGrid nhận tập trung (thông qua kèm theo tài sản/ràng buộc) nhưng bạn không thể sử dụng Up, Xuống, Page Up, Page Down chìa khóa để thay đổi hàng (SelectedItem). Nếu tôi tab một lần nữa, ô đầu tiên của hàng đầu tiên được hiển thị được chọn sẽ thay đổi SelectedItem.

Dòng dưới cùng, làm cách nào để tôi có thể tập trung bàn phím vào DataGridCell cho số SelectedItem khi số DataGrid nhận được tiêu điểm?

Có quá nhiều câu hỏi DataGrid/Focus và đã thử một vài điều. Cảm ơn bạn đã giúp đỡ.

Trả lời

7

Bạn cần cung cấp cho tiêu điểm lôgic hàng mới được chọn. Sau khi chọn mục mới cố gắng thay thế SetFocus cuộc gọi của bạn với điều này:

 var selectedRow = (DataGridRow)dataGrid1.ItemContainerGenerator.ContainerFromIndex(dataGrid1.SelectedIndex); 
     FocusManager.SetIsFocusScope(selectedRow, true); 
     FocusManager.SetFocusedElement(selectedRow, selectedRow); 
+0

Làm việc một cách hoàn hảo! Đặt điều này trong trình xử lý sự kiện GotKeyboardFocus và chỉ gọi nếu e.NewFocus là DataGrid. – KornMuffin

+0

@KornMuffin bạn đang sử dụng nó trong WPF mỏ không hoạt động tôi chỉ muốn tập trung vào DataGrid của tôi mà phím mũi tên làm việc nhưng nó không làm việc bất kỳ giải pháp xin vui lòng – Ahmad

+0

@Ahmad Có, WPF. DataGrid.SelectionUnit của tôi là FullRow. Không chắc chắn nếu điều đó sẽ tạo sự khác biệt cho bạn hay không. – KornMuffin

0

PowerShell này Đoạn làm việc cho tôi:

$dataGrid = ...  
$dataGrid.add_GotKeyboardFocus({ 
    param($Sender,$EventArgs) 
    if ($EventArgs.OldFocus -isnot [System.Windows.Controls.DataGridCell) { 
     $row = $dataGrid.ItemContainerGenerator.ContainerFromIndex($dataGrid.SelectedIndex) 
     $row.MoveFocus((New-Object System.Windows.Input.TraversalRequest("Next"))) 
    } 
}) 
0

Giải pháp FocusManager không làm việc cho tôi vì một lý do. Ngoài ra tôi yêu cầu một apporach chung hơn. Vì vậy, đây là, những gì tôi đã đưa ra:

using System.Windows.Controls; 

public static void RestoreFocus(this DataGrid dataGrid, 
            int column = 0, bool scrollIntoView = false) 
{ 
    if (dataGrid.IsKeyboardFocusWithin && dataGrid.SelectedItem != null) 
    { 
     // make sure everything is up to date 
     dataGrid.UpdateLayout(); 

     if (scrollIntoView) 
     { 
      dataGrid.ScrollIntoView(dataGrid.SelectedItem); 
     } 

     var cellcontent = dataGrid.Columns[column].GetCellContent(dataGrid.SelectedItem); 
     var cell = cellcontent?.Parent as DataGridCell; 
     if (cell != null) 
     { 
      cell.Focus(); 
     } 
    } 
} 

Và gọi nó là như thế này:

MyDataGrid.IsKeyboardFocusWithinChanged += (sender, e) => 
{ 
    if ((bool)e.NewValue == true) 
    { 
     Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Loaded, new Action(() => 
     { 
      MyDataGrid.RestoreFocus(scrollIntoView: true); 
     })); 
    } 
}; 
Các vấn đề liên quan