Click here to Skip to main content
12,500,818 members (58,601 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# WPF
Hello,

I have a combo box in WPF project page. I have several items in it and one of the items is '(None)'.
Since my combo box is a compulsory field I would like to force validation on the combo box and wish to throw new Validation error if '(None)' is selected which gives red border around the combo box, just like on text box validation.

Your guidance will be of great help as I am very new to WPF.

Thank you.
Posted 1-Dec-12 0:22am
Updated 1-Dec-12 2:25am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

private void button1_Click(object sender, RoutedEventArgs e)
      {
          foreach (ComboBoxItem i in comboBox1.Items)
          {
              if (i.IsSelected)
              {
                  // perform you desired your operations
              }
              else
              {
                  // throw validation messages
              }
          }
      }
  Permalink  
Comments
xrndMember 1-Dec-12 7:20am
   
How to throw validation error message for combo box,that is new ValidationError..
I have written
ValidationError validationError = new ValidationError(new ComboBoxError(),
mytext.GetBindingExpression(ComboBoxItem.ContentProperty), "Field Empty", null);
Validation.MarkInvalid(mytext.GetBindingExpression(ComboBoxItem.ContentProperty), validationError);

which doesnt work as desired.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi dear,
Read this message.
 
if(comboBox1.SelectedValue == null)
{
 // Validation message. 
 //errorProvider1.SetError(comboBox1, "comboBox value is required.");
}
else
{
 // Continue you desire work.
}
  Permalink  
v4
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

if(combobox1.selectedindex==0)
{
     //Validation error
}
else
{
    //True condition...
}

Thanks & regard
Sham:)
  Permalink  
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hey hi ,
I have finally found the solution.

I was supposed to bind my ComboBox text property with The validation error..
And then I provided the Buizness rule by simply creating the class in c#.
Yeah the Validation wasn't meant to be done when Cursor leavs the combobox but when I impose FORCE VALIDATION on my control..

Code in Page.Resources
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100