C# 如何禁用FlipView键盘按键而不在子控件上禁用它
故事中,我使用MahApps.Metro FlipView在我的窗口中,FlipView包含一个GridView,这很好地工作,但我有一个问题,我不能使用键盘箭头在GridView单元格中导航,C# 如何禁用FlipView键盘按键而不在子控件上禁用它,c#,wpf,xaml,mahapps.metro,C#,Wpf,Xaml,Mahapps.metro,故事中,我使用MahApps.Metro FlipView在我的窗口中,FlipView包含一个GridView,这很好地工作,但我有一个问题,我不能使用键盘箭头在GridView单元格中导航,向上和向下箭头工作,但左和右箭头不工作,当我按下它时,flipview会更改页面 我试着这样处理OnPreviewKeyPress private void FlipView_PreviewKeyDown(object sender, KeyEventArgs e) { e.Handled = t
向上和向下箭头工作,但左和右箭头不工作,当我按下它时,flipview会更改页面
我试着这样处理OnPreviewKeyPress
private void FlipView_PreviewKeyDown(object sender, KeyEventArgs e)
{
e.Handled = true;
}
但是GridView也不会收到按键。您尝试处理PreviewKeyDown
的方法是正确的。诀窍是,您需要手动触发DataGrid
的KeyDown
事件
下面是一个适用于我的测试用例的实现:
private void UIElement_PreviewKeyDown(object sender, KeyEventArgs e)
{
var dataGrid = FlipView.SelectedItem as DataGrid;
if (dataGrid == null) return; // the selected item is not a DataGrid
if (!dataGrid.SelectedCells.Any()) return; // no selected cells to move between
// create a new event args to send to the DataGrid
var args = new KeyEventArgs(
Keyboard.PrimaryDevice,
Keyboard.PrimaryDevice.ActiveSource,
0,
e.Key);
args.RoutedEvent = Keyboard.KeyDownEvent; // get the event
dataGrid.RaiseEvent(args); // raise the event
e.Handled = true; // prevent the FlipView from going forward/backward
}
假设:仅当(1)所选项目是数据网格且(2)所选的数据网格有一个或多个单元格时,您才希望抑制向前/向后移动FlipView
的功能。(否则,您希望箭头键正常工作。)
以下是我的测试用例的完整代码:
MainWindow.xaml.cs:
此外,DataGrid
的XAML应设置以下属性,以确保一次只能选择一个单元格:
<DataGrid ItemsSource="{Binding Items}"
AutoGenerateColumns="True"
SelectionMode="Single"
SelectionUnit="Cell"
IsReadOnly="True" />
我也很想知道这个问题的解决方案,几个月前我经历过,我到处寻找,但最后我放弃了你的解决方案,现在我遇到了一些问题,我想知道你是否愿意帮助我,我有几个flipview页面,每个页面都有一个gridview,现在你能帮我吗,当选定的单元格位于最左侧或最右侧时,我希望按箭头键使flipview更改页面,单元格上也有一些焦点边框,不使用代码时此边框不会出现。@iha,我编辑了我的答案,以允许在选择最左侧或最右侧单元格时使用箭头键翻转页面。我还提供了一个新的XAML代码片段来展示如何解决焦点边界的问题。焦点边界仍然可见,顺便说一句,我接受你的回答,并将在10分钟内奖励赏金谢谢,@iha!很抱歉,我无法解决边界问题。如果你问一个单独的问题,你可能会得到帮助。祝你好运
<Window x:Class="Sandbox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Sandbox"
xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
mc:Ignorable="d"
Title="MainWindow"
Width="525"
Height="350">
<controls:FlipView x:Name="FlipView"
Margin="0, 0, 10, 0"
Height="200"
IsBannerEnabled="True"
PreviewKeyDown="UIElement_PreviewKeyDown">
<controls:FlipView.Items>
<DataGrid ItemsSource="{Binding Items}"
AutoGenerateColumns="True" />
<Rectangle Margin="0, 0, 10, 0"
Width="50"
Height="50"
Fill="Red" />
<Rectangle Margin="0, 0, 10, 0"
Width="50"
Height="50"
Fill="Blue" />
</controls:FlipView.Items>
</controls:FlipView>
</Window>
private void UIElement_PreviewKeyDown(object sender, KeyEventArgs e)
{
var dataGrid = FlipView.SelectedItem as DataGrid;
if (dataGrid == null) return;
if (!dataGrid.SelectedCells.Any()) return;
// get the column index of the selected cell
var columnIndex = dataGrid.SelectedCells.First().Column.DisplayIndex;
// exit if the selected cell is left/right and the arrow is left/right
if (columnIndex == 0 && e.Key == Key.Left ||
columnIndex == dataGrid.Columns.Count - 1 && e.Key == Key.Right) return;
var args = new KeyEventArgs(
Keyboard.PrimaryDevice,
Keyboard.PrimaryDevice.ActiveSource,
0,
e.Key);
args.RoutedEvent = Keyboard.KeyDownEvent;
dataGrid.RaiseEvent(args);
e.Handled = true;
}
<DataGrid ItemsSource="{Binding Items}"
AutoGenerateColumns="True"
SelectionMode="Single"
SelectionUnit="Cell"
IsReadOnly="True" />