Style Dataform label based on validation severity with Silverlight 4
I am trying to extend the Silverlight Validation to include severity. I am taking the approach of adding a special character at the beginning of the message to indicate if it is an error or a warning, and changing the style to either red or blue. I have successfully implemented this for the Validation Summery control, input controls and ValidationToolTips 开发者_如何学运维by specifying a custom Style and using value converters. But I can't seem to get it to work with labels.
My problem is that I can’t seem to bind the validation message. I have tried the following without any success:
OR
Can someone please provide some help or suggest an alternative approach. Links and or sample code is greatly appreciated.
I've solved this by throwing exceptions of different types (the ValidationWarning exception for warnings). Each time when validation exception is thrown, the BindingValidationError event is triggered. In event handler I check for exception type and change the style depending on exception:
private void OnBindingValidationError(object sender, ValidationErrorEventArgs e)
{
var textBox = sender as TextBox;
if (textBox != null)
{
if (e.Error.Exception is ValidationWarning)
{
UpdateStyle(textBox, "TextBoxWithWarning");
}
else
{
UpdateStyle(textBox, "TextBoxWithError");
}
}
}
private static void UpdateStyle(TextBox textBox, string styleName)
{
var newStyle = (Style)Application.Current.Resources[styleName];
if (textBox.Style != newStyle)
{
textBox.Style = newStyle;
textBox.Focus();
}
}
精彩评论