[英]C# WPF KeyDown highlight + selected last row in datagrid
我正在開發一個應用程序,用戶可以使用簡單的信息(如產品名稱和價格)將產品添加到數據網格中,然后我想例如按鍵盤上的F4鍵,然后我要關注數據網格中的最后一項,這意味着選擇它並突出顯示該項目!
所以,伙計們,我該如何實現這一目標,我嘗試了一些解決方案,例如將選擇的索引設置為我的數據網格和類似的方法,但是它無法正常工作
謝謝大家,干杯
以編程方式突出顯示DataGrid
的行或單元格比僅設置SelectedIndex
或SelectedItem
屬性要復雜一些。
但是,可以通過訪問DataGrid
控件的可視用戶界面元素並在特定的DataGridCell
對象上調用UIElement.Focus()
方法來選擇並集中代碼中的一行,並獲得與使用鼠標時相同的行為,如下所述。以下博客文章。
如何在WPF中以編程方式選擇和突出顯示DataGrid中的行或單元格: https : //blog.magnusmontin.net/2013/11/08/how-to-programmatically-select-and-focus-a-row-or- WPF中的數據網格中的單元格/
這是一個例子:
public partial class MainWindow : Window
{
public MainWindow
{
InitializeComponent();
this.PreviewKeyDown += (s, e) =>
{
if(e.Key == Key.F4)
SelectRowByIndex(dataGridProducts, dataGridProducts.Items.Count - 1);
};
//populate DataGrid etc...
}
private static void SelectRowByIndex(DataGrid dataGrid, int rowIndex)
{
if (!dataGrid.SelectionUnit.Equals(DataGridSelectionUnit.FullRow))
throw new ArgumentException("The SelectionUnit of the DataGrid must be set to FullRow.");
if (rowIndex < 0 || rowIndex > (dataGrid.Items.Count - 1))
throw new ArgumentException(string.Format("{0} is an invalid row index.", rowIndex));
dataGrid.SelectedItems.Clear();
object item = dataGrid.Items[rowIndex];
dataGrid.SelectedItem = item;
DataGridRow row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
if (row == null)
{
/* bring the data item (Product object) into view
* in case it has been virtualized away */
dataGrid.ScrollIntoView(item);
row = dataGrid.ItemContainerGenerator.ContainerFromIndex(rowIndex) as DataGridRow;
}
if (row != null)
{
DataGridCell cell = GetCell(dataGrid, row, 0);
if (cell != null)
cell.Focus();
}
}
private static DataGridCell GetCell(DataGrid dataGrid, DataGridRow rowContainer, int column)
{
if (rowContainer != null)
{
System.Windows.Controls.Primitives.DataGridCellsPresenter presenter
= FindVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
if (presenter == null)
{
/* if the row has been virtualized away, call its ApplyTemplate() method
* to build its visual tree in order for the DataGridCellsPresenter
* and the DataGridCells to be created */
rowContainer.ApplyTemplate();
presenter = FindVisualChild<System.Windows.Controls.Primitives.DataGridCellsPresenter>(rowContainer);
}
if (presenter != null)
{
DataGridCell cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
if (cell == null)
{
/* bring the column into view
* in case it has been virtualized away */
dataGrid.ScrollIntoView(rowContainer, dataGrid.Columns[column]);
cell = presenter.ItemContainerGenerator.ContainerFromIndex(column) as DataGridCell;
}
return cell;
}
}
return null;
}
private static T FindVisualChild<T>(DependencyObject obj) where T : DependencyObject
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child != null && child is T)
return (T)child;
else
{
T childOfChild = FindVisualChild<T>(child);
if (childOfChild != null)
return childOfChild;
}
}
return null;
}
}
您可以使用InputBinding識別按下的F4鍵。
<Window.InputBindings>
<KeyBinding Key="F4"
Command="{Binding SelectLastItemCommand}" />
</Window.InputBindings>
您可以在此處查看如何選擇項目: DataGrid中的WPF Binding SelectedItem
你的問題在哪里? 處理按鈕事件還是突出顯示行? 似乎是后者,因此請看一下: https : //www.codeproject.com/Tips/773382/Row-Highlighting-in-WPF-Grids
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.