[英]Prevent jumping to next row when user hits Enter in RadGridView
I have a RadGridView
from Telerik UI for WPF with an editable column.我有一个来自RadGridView
UI 的 RadGridView,用于 WPF 和一个可编辑的列。 When user edits a cell in that column and hits Enter , the same cell in next row in the grid gets focus.当用户编辑该列中的单元格并点击Enter时,网格中下一行中的同一单元格将获得焦点。
I want the cell that the user just edited to lose focus.我希望用户刚刚编辑的单元格失去焦点。 How to do this MVVM-style?如何做到这种 MVVM 风格?
You can change this behavior by writing a custom keyboard command provider.您可以通过编写自定义键盘命令提供程序来更改此行为。 This provider overrides the default Enter
press action.此提供程序会覆盖默认的Enter
按下操作。 It commits the edits and deselects the cellby setting the SelectedItem
to null
.它通过将SelectedItem
设置为null
来提交编辑并取消选择单元格。
public class CustomKeyboardCommandProvider : DefaultKeyboardCommandProvider
{
private readonly GridViewDataControl _grid;
public CustomKeyboardCommandProvider(GridViewDataControl grid)
: base(grid)
{
_grid = grid;
}
public override IEnumerable<ICommand> ProvideCommandsForKey(Key key)
{
var commands = base.ProvideCommandsForKey(key).ToList();
if (key != Key.Enter)
return commands;
commands.Clear();
commands.Add(RadGridViewCommands.CommitEdit);
_grid.SelectedItem = null;
return commands;
}
}
You have to assign the new provider.您必须分配新的提供者。 The easiest way is in code-behind, as it has a constructor parameter.最简单的方法是在代码隐藏中,因为它有一个构造函数参数。
MyRadDataGridView.KeyboardCommandProvider = new CustomKeyboardCommandProvider(TestGridView);
Alternatively, create a TriggerAction<RadGridView>
and attach it to the Loaded
event of the grid view.或者,创建一个TriggerAction<RadGridView>
并将其附加到网格视图的Loaded
事件。
public class SetCustomKeyboardCommandProviderAction : TriggerAction<RadGridView>
{
protected override void Invoke(object parameter)
{
AssociatedObject.KeyboardCommandProvider = new CustomKeyboardCommandProvider(AssociatedObject);
}
}
<telerik:RadGridView>
<b:Interaction.Triggers>
<b:EventTrigger EventName="Loaded">
<local:SetCustomKeyboardCommandProviderAction/>
</b:EventTrigger>
</b:Interaction.Triggers>
<!-- ...other definitions. -->
</telerik:RadGridView>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.