gpt4 book ai didi

c# - WPF C# - DataGrid 在用户按键上选择项目

转载 作者:行者123 更新时间:2023-11-30 21:53:21 25 4
gpt4 key购买 nike

我创建了这个函数来在用户按键时选择 DataGrid 上的项目。如果用户键是“A”,它将选择用户名以字母“A”开头的第一项。如果用户 key 再次为“A”,它将选择用户名以字母“A”开头的下一个项目,依此类推。该功能效果很好,但我想要的是当不再有用户名以“A”开头的项目重新开始时(选择第一个项目),它目前保留在用户名以字母“A”开头的最后一个项目上。

private static Key lastKey;
private static int lastFoundIndex = 0;

public static void AccountsDataGrid_SearchByKey(object sender, KeyEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;

if ((dataGrid.Items.Count == 0) && !(e.Key >= Key.A && e.Key <= Key.Z))
{
return;
}

if ((lastKey != e.Key) || (lastFoundIndex == dataGrid.Items.Count - 1))
{
lastFoundIndex = 0;
}

for (int i = lastFoundIndex; i < dataGrid.Items.Count; i++)
{
if (dataGrid.SelectedIndex == i)
{
continue;
}

Account account = dataGrid.Items[i] as Account;

if (account.Username.StartsWith(e.Key.ToString(), true, CultureInfo.CurrentCulture))
{
dataGrid.ScrollIntoView(account);
dataGrid.SelectedItem = account;

lastFoundIndex = i;

break;
}
}

lastKey = e.Key;
}

更新(带解决方案):

受 Danielle 想法的启发,我更改了如下代码,效果非常好。

private static Key lastKey;
private static int lastFoundIndex = 0;

public static void AccountsDataGrid_SearchByKey(object sender, KeyEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;

if ((dataGrid.Items.Count == 0) && !(e.Key >= Key.A && e.Key <= Key.Z))
{
return;
}

if ((lastKey != e.Key) || (lastFoundIndex == dataGrid.Items.Count - 1))
{
lastFoundIndex = 0;
}

Func<object, bool> itemCompareMethod = (item) =>
{
Account account = item as Account;

if (account.Username.StartsWith(e.Key.ToString(), true, CultureInfo.CurrentCulture))
{
return true;
}

return false;
};

lastFoundIndex = FindDataGridRecordWithinRange(dataGrid, lastFoundIndex, dataGrid.Items.Count, itemCompareMethod);

if (lastFoundIndex == -1)
{
lastFoundIndex = FindDataGridRecordWithinRange(dataGrid, 0, dataGrid.Items.Count, itemCompareMethod);
}

if (lastFoundIndex != -1)
{
dataGrid.ScrollIntoView(dataGrid.Items[lastFoundIndex]);
dataGrid.SelectedIndex = lastFoundIndex;
}

if (lastFoundIndex == -1)
{
lastFoundIndex = 0;
dataGrid.SelectedItem = null;
}

lastKey = e.Key;
}

private static int FindDataGridRecordWithinRange(DataGrid dataGrid, int min, int max, Func<object, bool> itemCompareMethod)
{
for (int i = min; i < max; i++)
{
if (dataGrid.SelectedIndex == i)
{
continue;
}

if (itemCompareMethod(dataGrid.Items[i]))
{
return i;
}
}

return -1;
}

最佳答案

您最终采用的解决方案过于复杂,并且会检查不需要检查的行。也不需要维护状态的两个静态变量。试试这个:

    public void MainGrid_SearchByKey(object sender, KeyEventArgs e)
{
DataGrid dataGrid = sender as DataGrid;
if (dataGrid.Items.Count == 0 || e.Key < Key.A || e.Key > Key.Z)
{
return;
}

Func<object, bool> doesItemStartWithChar = (item) =>
{
Account account = item as Account;
return account.Username.StartsWith(e.Key.ToString(), true, CultureInfo.InvariantCulture);
};

int currentIndex = dataGrid.SelectedIndex;
int foundIndex = currentIndex;

// Search in following rows
foundIndex = FindMatchingItemInRange(dataGrid, currentIndex, dataGrid.Items.Count - 1, doesItemStartWithChar);

// If not found, search again in the previous rows
if (foundIndex == -1)
{
foundIndex = FindMatchingItemInRange(dataGrid, 0, currentIndex - 1, doesItemStartWithChar);
}

if (foundIndex > -1) // Found
{
dataGrid.ScrollIntoView(dataGrid.Items[foundIndex]);
dataGrid.SelectedIndex = foundIndex;
}
}

private static int FindMatchingItemInRange(DataGrid dataGrid, int min, int max, Func<object, bool> doesItemStartWithChar)
{
for (int i = min; i <= max; i++)
{
if (dataGrid.SelectedIndex == i) // Skip the current selection
{
continue;
}

if (doesItemStartWithChar(dataGrid.Items[i])) // If current item matches the string, return index
{
return i;
}
}

return -1;
}

关于您的评论,只需添加此检查:

        if (Keyboard.Modifiers.HasFlag(ModifierKeys.Control))
{
return;
}

关于c# - WPF C# - DataGrid 在用户按键上选择项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33923303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com