C# DataGridRow出错时阻止所有控件
我正在从事一个WPF项目,现在我正在验证C# DataGridRow出错时阻止所有控件,c#,wpf,datagrid,C#,Wpf,Datagrid,我正在从事一个WPF项目,现在我正在验证数据网格中的数据。因此,当插入无效数据时,我会在行标题中显示一个通知图像。到目前为止一切进展顺利 但我的问题是:除了DataGrid的当前行之外,当插入“无效”数据时,是否有方法阻止应用程序中的任何其他控件??或者,在输入正确数据之前,如何防止当前行失去焦点?? 到目前为止,我的想法是使用eventAggregator引发一个事件,将错误通知所有控件。但这很难,因为我必须在每个控件中使用一种方法 希望有人能帮助我,提前谢谢你。取消CellEditEndin
数据网格中的数据。因此,当插入无效数据时,我会在行标题中显示一个通知图像。到目前为止一切进展顺利
但我的问题是:除了DataGrid的当前行之外,当插入“无效”数据时,是否有方法阻止应用程序中的任何其他控件??或者,在输入正确数据之前,如何防止当前行失去焦点??
到目前为止,我的想法是使用eventAggregator
引发一个事件,将错误通知所有控件。但这很难,因为我必须在每个控件中使用一种方法
希望有人能帮助我,提前谢谢你。取消CellEditEnding事件可以阻止手机失去焦点:
public MainWindow()
{
InitializeComponent();
dataGrid1.ItemsSource = new List<TestClass>() { new TestClass() };
dataGrid1.CellEditEnding += new EventHandler<DataGridCellEditEndingEventArgs>(dataGrid1_CellEditEnding);
}
void dataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if(whateveryouwant == true)
return;
else
e.Cancel = true;
}
public主窗口()
{
初始化组件();
dataGrid1.ItemsSource=new List(){new TestClass()};
dataGrid1.CellEditEnding+=新事件处理程序(dataGrid1\u CellEditEnding);
}
void dataGrid1_CellEditEnding(对象发送方,DataGridCellEditEndingEventArgs e)
{
if(whateveryouwant==true)
返回;
其他的
e、 取消=真;
}
编辑:
EventAggregator是解决此问题的一个好方法,但由于您知道但似乎不喜欢它,下面是一个更简单的方法,尽管您必须指定一些应该能够停止的控件类型:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
dataGrid1.ItemsSource = new List<TestClass>() { new TestClass() };
dataGrid1.CellEditEnding += new EventHandler<DataGridCellEditEndingEventArgs>(dataGrid1_CellEditEnding);
MouseDownHandler = new MouseButtonEventHandler((sender, args) => { args.Handled = true; });
MouseClickHandler = new RoutedEventHandler((sender, args) => { args.Handled = true; });
}
private bool IsMouseEventStopped = false;
private RoutedEventHandler MouseClickHandler = null;
private MouseButtonEventHandler MouseDownHandler = null;
void dataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
bool correctCellValue = false;
//correctCellValue = true to stop editing, if the cell value is correct
if (correctCellValue)
{
// unblock mouse events
if (IsMouseEventStopped == true)
{
foreach (Button c in FindVisualChildren<Button>(this))
c.Click -= MouseClickHandler;
foreach (TextBox c in FindVisualChildren<TextBox>(this))
c.PreviewMouseLeftButtonDown -= MouseDownHandler;
}
IsMouseEventStopped = false;
}
else
{
e.Cancel = true;
// block mouse events to certain controls
if (IsMouseEventStopped == false)
{
IsMouseEventStopped = true;
foreach (Button c in FindVisualChildren<Button>(this))
c.Click += MouseClickHandler;
foreach (TextBox c in FindVisualChildren<TextBox>(this))
c.PreviewMouseLeftButtonDown += MouseDownHandler;
}
}
}
public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
if (depObj != null)
{
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
if (child != null && child is T)
yield return (T)child;
foreach (T childOfChild in FindVisualChildren<T>(child))
yield return childOfChild;
}
}
}
}
公共部分类主窗口:窗口
{
公共主窗口()
{
初始化组件();
dataGrid1.ItemsSource=new List(){new TestClass()};
dataGrid1.CellEditEnding+=新事件处理程序(dataGrid1\u CellEditEnding);
MouseDownHandler=newmousebuttoneventhandler((发送方,args)=>{args.Handled=true;});
MouseClickHandler=newroutedeventhandler((发送方,args)=>{args.Handled=true;});
}
private bool IsMouseEventStopped=false;
专用路由EventHandler MouseClickHandler=null;
私有MouseButtonEventHandler MouseDownHandler=null;
void dataGrid1_CellEditEnding(对象发送方,DataGridCellEditEndingEventArgs e)
{
bool correctCellValue=false;
//correctCellValue=如果单元格值正确,则停止编辑
if(校正单元值)
{
//取消阻止鼠标事件
if(IsMouseEventStopped==true)
{
foreach(FindVisualChildren中的按钮c(此))
c、 单击-=鼠标单击鼠标指针;
foreach(FindVisualChildren中的文本框c(本))
c、 PreviewMouseLeftButtonDown-=MouseDownHandler;
}
IsMouseEventStopped=false;
}
其他的
{
e、 取消=真;
//将鼠标事件阻止到某些控件
if(IsMouseEventStopped==false)
{
IsMouseEventStopped=true;
foreach(FindVisualChildren中的按钮c(此))
c、 单击+=鼠标单击鼠标指针;
foreach(FindVisualChildren中的文本框c(本))
c、 PreviewMouseLeftButtonDown+=MouseDownHandler;
}
}
}
公共静态IEnumerable FindVisualChildren(DependencyObject depObj),其中T:DependencyObject
{
if(depObj!=null)
{
for(int i=0;i
感谢Bryce Kahle为FindVisualChildren提供的帮助是的,我已经尝试过了,但是我需要我的整个应用程序都没有响应,除了我当前的行有响应之外error@Dante这种方法一开始似乎是个好主意,但很快就会让用户产生敌意。当应用程序强制用户在此处填写这些数据时,用户会感到困惑和沮丧。不同的人会有不同的流,可能希望以稍微不同的顺序插入数据。很多时候,我注意到人们会输入虚拟数据,目的是在几秒钟后修复它。你可能想考虑一个设计,在数据正确之前,他们不能点击“接受”按钮,但仍然可以使用其他控件。+ 1给Pete Baughman,我只是添加了它作为可能的答案,但是我也不太喜欢它。更好的ui设计模式是,如果单元格在值不正确时失去焦点,则恢复单元格值。或者可以高亮显示红色单元格,直到输入正确的值。