Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8193575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:31:22+00:00 2026-06-07T04:31:22+00:00

I am working on a WPF project, and right now I am validating data

  • 0

I am working on a WPF project, and right now I am validating data in a DataGrid. so, when invalid data is inserted I show a notification image in the RowHeader. Everything goes well so far.

But my question is: Is there a way to block any other control in the apllication when “Invalid” data is inserted excepting the current row of the DataGrid?? Or, what can I do to prevent the current row from lose the focus until correct data is entered??

So far my idea is to Raise an event using eventAggregator to inform all the controls about the error. But this is hard since I would have to suscribe a method in each control I could have.

Hope someone can help me, thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T04:31:25+00:00Added an answer on June 7, 2026 at 4:31 am

    by canceling the CellEditEnding Event you stop the cell from losing focus:

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

    EDIT:

    EventAggregator is a good way to solve it, but since you know that but seem to not like it, a simpler way would be following, though you would have to specify a few types of controls that should be able to be stopped:

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

    thanks to Bryce Kahle for the FindVisualChildren from here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on a WPF MVVM Project. Now i have a DataGrid in
I am working on a WPF project where I have to import data from
I'm working on a WPF project using MVVM and I'm trying to implement a
I am working on a WPF project. And I just created a dependency property.
I am working on a WPF project, and I am trying to fire an
I am working on a WPF project, using MVVM and Microsoft Prism libraries .
I'm working on a multi-lingual WPF project that will be localized into many different
I have been working on a C# 4.0 WPF project and need to figure
I'm working on the UI side of a WPF project. My favourite reference while
I would like to use an MVVM in a WPF project I'm working on,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.