在 WPF 数据网格中按下一个键后,如何让用户跳转到下一个合适的项目?
How do I let the user jump to the next fitting item after pressing a key in an WPF Data grid?
这是我使用的 WPF-Datagrid:
<DataGrid Grid.Row="1" x:Name="products" CanUserAddRows="true" MouseDoubleClick="products_MouseDoubleClick" InitializingNewItem="products_InitializingNewItem" IsReadOnly="True"/>
产品数据集有一个名为“昵称”的 属性,并按此 属性 排序。当用户按下一个字母(例如“m”)时,我希望我的数据网格 select 下一行的昵称以“m”开头。
简而言之:我希望我的数据网格显示标准行为,就像 Windows(例如资源管理器)中的大多数数据网格一样。
此代码:
private void products_KeyUp(object sender, KeyEventArgs e)
{
foreach (ProductDatasource item in products.Items)
{
if (item.Nickname.StartsWith(e.Key.ToString()))
{
Debug.Print("Found it, but how to set the selection?");
}
}
}
将找到以该字母开头的第一个项目。但是我怎么才能select这个项目呢?
而且 windows 中的每个 Grid 都表现出相同的行为(甚至以更复杂的方式,例如从当前位置开始搜索;如果您紧接着按 2 个键,例如“mi”,它会搜索对于以“mi”开头的下一个项目,我想知道 DataGrid 是否内置了我可以用来执行此操作的行为?
设置 SelectedItem
属性 的 DataGrid
:
private void products_KeyUp(object sender, KeyEventArgs e)
{
foreach (ProductDatasource item in products.Items)
{
if (item.Nickname.StartsWith(e.Key.ToString()))
{
products.SelectedItem = item;
}
}
}
这是我使用的 WPF-Datagrid:
<DataGrid Grid.Row="1" x:Name="products" CanUserAddRows="true" MouseDoubleClick="products_MouseDoubleClick" InitializingNewItem="products_InitializingNewItem" IsReadOnly="True"/>
产品数据集有一个名为“昵称”的 属性,并按此 属性 排序。当用户按下一个字母(例如“m”)时,我希望我的数据网格 select 下一行的昵称以“m”开头。
简而言之:我希望我的数据网格显示标准行为,就像 Windows(例如资源管理器)中的大多数数据网格一样。
此代码:
private void products_KeyUp(object sender, KeyEventArgs e)
{
foreach (ProductDatasource item in products.Items)
{
if (item.Nickname.StartsWith(e.Key.ToString()))
{
Debug.Print("Found it, but how to set the selection?");
}
}
}
将找到以该字母开头的第一个项目。但是我怎么才能select这个项目呢?
而且 windows 中的每个 Grid 都表现出相同的行为(甚至以更复杂的方式,例如从当前位置开始搜索;如果您紧接着按 2 个键,例如“mi”,它会搜索对于以“mi”开头的下一个项目,我想知道 DataGrid 是否内置了我可以用来执行此操作的行为?
设置 SelectedItem
属性 的 DataGrid
:
private void products_KeyUp(object sender, KeyEventArgs e)
{
foreach (ProductDatasource item in products.Items)
{
if (item.Nickname.StartsWith(e.Key.ToString()))
{
products.SelectedItem = item;
}
}
}