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
<cod>
<ControlTemplate x:Key="ComboBoxValidationErrorTamplate">
<DockPanel>
<Border BorderBrush="Red" BorderThickness="2">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
Code For Control (ComboBox)
<combobox grid.row="2" height="23" horizontalalignment="Left" margin="6">
x:Name="cmbType" VerticalAlignment="Top" Width="168" Grid.Column="3"
Validation.ErrorTemplate="{StaticResource ComboBoxValidationErrorTamplate}" Validation.Error="NumberError">
<combobox.text>
<binding path="Type" notifyonvalidationerror="True" mode="OneWayToSource">
<binding.validationrules>
<exceptionvalidationrule />
</binding.validationrules>
</binding>
</combobox.text>
<comboboxitem content="(None)" />
<comboboxitem content="Electrical" />
</combobox>
Code in .cs file
private void NumberError(object sender, ValidationErrorEventArgs e)
{
if (e.Action == ValidationErrorEventAction.Added)
{
((System.Windows.Controls.Control)sender).ToolTip = e.Error.ErrorContent.ToString();
}
else
{
((System.Windows.Controls.Control)sender).ToolTip = "";
}
}
private void errorCheckComboBox(System.Windows.Controls.Control controlName)
{
var mytext1 = controlName as System.Windows.Controls.ComboBox;
ComboBoxItem mytext = (ComboBoxItem)mytext1.SelectedItem;
if (mytext.Content.ToString().Trim().Equals("(None)"))
{
new ValidationResult(false, "Selection is invalid.");
ValidationError validationError =
new ValidationError(new ComboBoxError(),
controlName.GetBindingExpression(System.Windows.Controls.ComboBox.TextProperty), "Field Empty", null);
validationError.ErrorContent = "Please Select Valid Option";
Validation.MarkInvalid(
controlName.GetBindingExpression(System.Windows.Controls.ComboBox.TextProperty),
validationError);
ErrorExists = true;
} }
public class ComboBoxError: ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
if (value is ComboBoxItem)
{
if (value.ToString().Equals("(None)"))
{
return new ValidationResult(false, "Selection is invalid.");
}
}
else
{
return new ValidationResult(true, null);
}
return new ValidationResult(true, null);
}
}
errorCheckComboBox(cmbType);
This works abs fine nw!!