C# WPF DataGrid禁用单元格正在接收文本输入
考虑以下具有三列的DataGrid: 当年龄为-1时,相应的单元格将被禁用 理想情况下,用户不可能更改禁用的单元格值。然而,考虑用户在第1行,键盘焦点在列年龄的相应单元格中,然后按Enter,现在用户键入任何数字,而禁用的单元格得到该值!这是一种期望的行为吗?我怎样才能避免这种行为 要复制此问题,请执行以下操作:C# WPF DataGrid禁用单元格正在接收文本输入,c#,wpf,datagrid,isenabled,datagridcell,C#,Wpf,Datagrid,Isenabled,Datagridcell,考虑以下具有三列的DataGrid: 当年龄为-1时,相应的单元格将被禁用 理想情况下,用户不可能更改禁用的单元格值。然而,考虑用户在第1行,键盘焦点在列年龄的相应单元格中,然后按Enter,现在用户键入任何数字,而禁用的单元格得到该值!这是一种期望的行为吗?我怎样才能避免这种行为 要复制此问题,请执行以下操作: 选择“年龄”列第1行中的单元格 按回车键 键入一个数字 可复制代码: XAML: 对应的cs: using System.Collections.Generic; using
对应的cs:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
namespace wpf_behaviour
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
List<User> users = new List<User>();
users.Add(new User() { Id = 1, Name = "Kumar", Age = 10 });
users.Add(new User() { Id = 2, Name = "Sameer", Age = -1 });
users.Add(new User() { Id = 3, Name = "Danny", Age= 16 });
dgUsers.ItemsSource = users;
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
}
使用System.Collections.Generic;
使用System.Windows;
使用System.Windows.Documents;
名称空间wpf_行为
{
///
///MainWindow.xaml的交互逻辑
///
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
列表用户=新列表();
添加(新用户(){Id=1,Name=“Kumar”,Age=10});
添加(新用户(){Id=2,Name=“Sameer”,Age=-1});
添加(新用户(){Id=3,Name=“Danny”,Age=16});
dgUsers.ItemsSource=用户;
}
公共类用户
{
公共int Id{get;set;}
公共字符串名称{get;set;}
公共整数{get;set;}
}
}
}
我不确定为什么会发生这种情况,但您可以捕获Enter事件并取消编辑:
C#
XAML
我得到了解决方案(添加了PreviewKeyDown事件处理程序),现在就是了,我也想知道更好的解决方案:
private void DataGridCell_PreviewKeyDown(object sender, KeyEventArgs e)
{
try
{
DataGridCell cl = (DataGridCell)sender;
//Get the Cell's parent row
//using System.Windows.Media; for VisaualTreeHelper
var DataGridRowParent = VisualTreeHelper.GetParent(cl);
while (DataGridRowParent != null && DataGridRowParent.GetType() != typeof(DataGridRow))
{
DataGridRowParent = VisualTreeHelper.GetParent(DataGridRowParent);
}
//Get the Row's parent DataGrid
var DataGridParent = VisualTreeHelper.GetParent(DataGridRowParent);
while (DataGridParent != null && DataGridParent.GetType() != typeof(DataGrid))
{
DataGridParent = VisualTreeHelper.GetParent(DataGridParent);
}
DataGrid dp = DataGridParent as DataGrid;
//Get the CurrentCell value of DataGrid
DataGridCellInfo cli = dp.CurrentCell;
var CellContent = cli.Column.GetCellContent(cli.Item);
if (CellContent != null)
{
//Get DataGridCell of DataGridCellInfo
DataGridCell dgc = (DataGridCell)CellContent.Parent;
if (dgc.IsEnabled == false)
{
//If the key pressed is Enter or Tab allow
if (e.Key == Key.Enter || e.Key == Key.Tab)
{
e.Handled = false;
return;
}
//If any other key is pressed don't allow.
e.Handled = true;
return;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
看看这篇帖子@S.Akbari设置
没有帮助!我不想取消所有的键。请输入。接收vale的禁用单元的所有事件均未触发。因此,我完全没有办法确定接收文本的单元格是否是禁用的单元格。
private void MyDataGrid_OnKeyDown(object sender, KeyEventArgs e)
{
var dg = sender as DataGrid;
// alter this condition for whatever valid keys you want - avoid arrows/tab, etc.
if (dg != null && !dg.IsReadOnly && e.Key == Key.Enter)
{
dg.CancelEdit();
e.Handled = true;
}
}
<DataGrid Grid.Column="1" Name="dgUsers" AutoGenerateColumns="False" PreviewKeyDown="MyDataGrid_OnKeyDown">
private void DataGridCell_PreviewKeyDown(object sender, KeyEventArgs e)
{
try
{
DataGridCell cl = (DataGridCell)sender;
//Get the Cell's parent row
//using System.Windows.Media; for VisaualTreeHelper
var DataGridRowParent = VisualTreeHelper.GetParent(cl);
while (DataGridRowParent != null && DataGridRowParent.GetType() != typeof(DataGridRow))
{
DataGridRowParent = VisualTreeHelper.GetParent(DataGridRowParent);
}
//Get the Row's parent DataGrid
var DataGridParent = VisualTreeHelper.GetParent(DataGridRowParent);
while (DataGridParent != null && DataGridParent.GetType() != typeof(DataGrid))
{
DataGridParent = VisualTreeHelper.GetParent(DataGridParent);
}
DataGrid dp = DataGridParent as DataGrid;
//Get the CurrentCell value of DataGrid
DataGridCellInfo cli = dp.CurrentCell;
var CellContent = cli.Column.GetCellContent(cli.Item);
if (CellContent != null)
{
//Get DataGridCell of DataGridCellInfo
DataGridCell dgc = (DataGridCell)CellContent.Parent;
if (dgc.IsEnabled == false)
{
//If the key pressed is Enter or Tab allow
if (e.Key == Key.Enter || e.Key == Key.Tab)
{
e.Handled = false;
return;
}
//If any other key is pressed don't allow.
e.Handled = true;
return;
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}