2

我正在开发一个 WPF 项目,现在我正在验证DataGrid. 因此,当插入无效数据时,我会在RowHeader. 到目前为止一切顺利。

但我的问题是:除了DataGrid的当前行之外,当插入“无效”数据时,有没有办法阻止应用程序中的任何其他控件?或者,我该怎么做才能防止当前行失去焦点,直到输入正确的数据?

到目前为止,我的想法是引发一个事件,eventAggregator用于通知所有控件有关该错误的信息。但这很难,因为我必须在我可以拥有的每个控件中订阅一个方法。

希望有人可以帮助我,在此先感谢您。

4

1 回答 1

1

通过取消 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;
}

编辑:

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;
            }
        }
    }
}

感谢 Bryce Kahle 在这里的 FindVisualChildren

于 2012-07-07T23:50:23.973 回答