當下面的DataGrid首次獲取焦點並且只是第一次(即,在某個其他控件獲得焦點後)時,最後一行第二列應該被聚焦並進行編輯。DataGrid以編程方式選擇最後一個單元
我增加了對DataGrid.GotFocus的處理程序,但它是複雜的代碼,並沒有得到上面的結果。
任何人都有一個優雅的防彈解決方案?
乾杯,
Berryl
編輯
我做了小小的修改代碼
- 發送者應該總是我想要的網格,所以我只是用依託,與其一個名字
當SelectionUnit是FullRow時,由於之前我的網格已將其更改爲CellOrRowHeader,因此您需要 ap parently不能打電話SelectedCells.Clear()
私人無效OnDataGridKeyboardGotFocus(對象發件人,KeyboardFocusChangedEventArgs E) {
var dg = sender as DataGrid; if (_hasHadInitialFocus) return; var rowIndex = dg.Items.Count - 2; if (rowIndex >= 0 && dg.Columns.Count - 1 >= 0) { var column = dg.Columns[dg.Columns.Count - 1]; var item = dg.Items[rowIndex]; var dataGridCellInfo = new DataGridCellInfo(item, column); if (dg.SelectionUnit != DataGridSelectionUnit.FullRow) { dg.SelectedCells.Clear(); dg.SelectedCells.Add(dataGridCellInfo); } else { var row = dg.GetRow(rowIndex); row.IsSelected = true; } dg.CurrentCell = dataGridCellInfo; dg.BeginEdit(); } _hasHadInitialFocus = true;
}
新問題
我想當焦點轉到窗口中的另一個控件然後返回到網格時重複選擇。 我想我可以把那_hasHadInitialFocus鎖定到虛假的LostFocus事件,但下面的代碼是射擊細胞的變化。 你知道我應該如何俘獲失去焦點的事件更好,你是否同意是把鎖住的地方嗎?
private void DataGridLostFocus(object sender, RoutedEventArgs e) {
_hasHadInitialFocus = false;
}
另一個新問題!
我找到的DataGrid是一個皮塔餅和短期上有用的信息。最好的鏈接(Vin Sibal,Jamie Rodriquez)都很舊(當它是ToolKit的一部分時)。
你知道的任何更新後的基準是真的好?
也許你可以看看e.NewFocus,e.OldFocus,e.OriginalSource在DataGridLostFocus,做一些聰明的那些。我擔心DataGrid上的信息來源可能與您的相同。 MSDN,StackOverflow和大量的實驗。 – Phil 2012-03-04 09:35:52