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 7851941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:18:02+00:00 2026-06-02T19:18:02+00:00

I have a form with a GroupBox, and in it I have a number

  • 0

I have a form with a GroupBox, and in it I have a number of controls (Checkboxes, TextBoxes, and Comboboxes).

The form is bound to a view model that implements IDataErrorInfo on its properties, and when the user enters an invalid value into a control, IDataInfo returns an invalid result, and the control is surrounded by the usual red box, and the error message is displayed at the bottom of the form.

The thing is, the GroupBox is intended to indicate a set of mandatory values. The user is required to check at least one of the checkboxes in the group. Failure to do so isn’t a error on an individual control, it’s an error on the group. So I’ve added a BindingGroup to the GroupBox, and added a ValidationRule that returns an error if nothing is selected. And that works fine. If nothing is selected the GroupBox is surrounded by the usual red box, and the error message is displayed at the bottom of the form.

My problem is that if one of the controls in the GroupBox fails validation, I get two red boxes – one around the control and one around the GroupBox. And I get two error messages in the list at the bottom of the form.

How do I keep the BindingGroup from reporting errors on everything that is contained within the group?

EDITED:

A simple example – this doesn’t display Validation.Errors, but you can see that the StackPanel is highlighted as having failed validation, when the contained TextBox does.

The XAML:

<Window
        x:Class="BugHunt5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:BugHunt5"
        Title="MainWindow"
        Height="350"
        Width="525"
        >
    <GroupBox 
            Margin="20"
            Header="This is my group"
            x:Name="MyGroupBox"
            >
        <StackPanel>
            <StackPanel.BindingGroup>
                <BindingGroup NotifyOnValidationError="True">
                </BindingGroup>
            </StackPanel.BindingGroup>
            <TextBox 
                    Height="30"
                    Width="100"
                    >
                <TextBox.Text>
                    <Binding
                            NotifyOnValidationError="True"
                            ValidatesOnDataErrors="True"
                            Path="MyString"
                            UpdateSourceTrigger="PropertyChanged"
                            >
                        <Binding.ValidationRules>
                            <local:NoDecimalsValidationRule ValidatesOnTargetUpdated="True"/>
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
        </StackPanel>
    </GroupBox>
</Window>

The C#:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        this.DataContext = new ViewModel("This should be an integer");
    }
}
public class ViewModel
{
    public string MyString
    { get; set; }
    public ViewModel(string mystring)
    { this.MyString = mystring; }
}
public class NoDecimalsValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value,
        System.Globalization.CultureInfo cultureInfo)
    {
        string myString = value as string;
        int result;
        if (!Int32.TryParse(myString, out result))
            return new ValidationResult(false, "Must enter integer");
        return new ValidationResult(true, null);
    }
}
  • 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-02T19:18:04+00:00Added an answer on June 2, 2026 at 7:18 pm

    ViewModel.cs

    public class ViewModel : INotifyPropertyChanged, IDataErrorInfo
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private bool checked1, checked2;
        private string myString;
    
        public bool Checked1
        {
            get { return this.checked1; }
            set { this.SetValue(ref this.checked1, value, "Checked1"); }
        }
    
        public bool Checked2
        {
            get { return this.checked2; }
            set { this.SetValue(ref this.checked2, value, "Checked2"); }
        }
    
        public string MyString
        {
            get { return this.myString; }
            set { this.SetValue(ref this.myString, value, "MyString"); }
        }
    
        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
                handler(this, e);
        }
    
        private void SetValue<T>(ref T field, T value, string propertyName)
        {
            if (!EqualityComparer<T>.Default.Equals(field, value))
            {
                field = value;
                this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
            }
        }
    
        public string Error
        {
            get
            {
                return this.checked1 == false && this.checked2 == false ? "Must check one value." : string.Empty;
            }
        }
    
        string IDataErrorInfo.this[string propertyName]
        {
            get
            {
                switch (propertyName)
                {
                    case "MyString":
                        int result;
                        return int.TryParse(this.myString, out result) ? string.Empty : "Must enter integer.";
                    default:
                        return string.Empty;
                }
            }
        }
    }
    

    MainWindow.xaml

    <Window x:Class="WpfApplication.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication"
            Title="MainWindow" Height="350" Width="525">
        <GroupBox Header="This is my group">
            <GroupBox.DataContext>
                <local:ViewModel MyString="This should be an integer"/>
            </GroupBox.DataContext>
            <StackPanel>
                <StackPanel.BindingGroup>
                    <BindingGroup x:Name="checkedBindingGroup">
                        <BindingGroup.ValidationRules>
                            <DataErrorValidationRule ValidationStep="ConvertedProposedValue"/>
                        </BindingGroup.ValidationRules>
                    </BindingGroup>
                </StackPanel.BindingGroup>
                <CheckBox IsChecked="{Binding Checked1, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" Binding.SourceUpdated="OnCheckedSourceUpdated" Content="Checked1"/>
                <CheckBox IsChecked="{Binding Checked2, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" Binding.SourceUpdated="OnCheckedSourceUpdated" Content="Checked2"/>
                <TextBox Text="{Binding MyString, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, BindingGroupName=Dummy}"/>
            </StackPanel>
        </GroupBox>
    </Window>
    

    MainWindow.xaml.cs

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    
        private void OnCheckedSourceUpdated(object sender, DataTransferEventArgs e)
        {
            this.checkedBindingGroup.ValidateWithoutUpdate();
        }
    }
    

    Key things:

    • Set BindingGroupName on ‘MyString’ Binding to some dummy value, so it isn’t contained in the parent BindingGroup.
    • ‘Checked1’ and ‘Checked2’ Bindings must set NotifyOnSourceUpdated to true and add event handler to Binding.SourceUpdated event in which BindingGroup validation has to be invoked explicitly.
    • ValidationStep of DataErrorValidationRule in BindingGroup must be ConvertedProposedValue or RawProposedValue so that BindingGroup.ValidateWithoutUpdate() executes validation logic.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

VB.NET 2008 windows app form. I have a groupbox with several checkboxes, comboboxes, and
I have a data entry form, with its DataCountext bound to a ViewModel object.
I have form for file uploading in my website that i check mime-type of
I have form with dateTimeField, and ListView. ListView looks like that: final ListView<String> countryView
I have a form that has a panel in it. I've set the panel
I have form with checkboxes loaded from database (I use entity field type). Checkboxes
I have a form holding a TableLayout with 1 column and 3 rows that
I have a form that appears as shown in the attached image. I have
I have several textboxes in a winform, some of them are inside a groupbox.
I'm currently writing some methods that do some basic operations on form controls eg

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.