Hello,
In one of page I have a Combo the XAML is given below.
Since user can Type-in any value in the combobox, in the LostFocus event, I am checking the text and if it's not from within the list, then I am clearing it. Below is the code for the event
Where I am binding ItemsSource with List<SelectItem>, SelectItem is a custom class with two property, Code and Description.
And in the Entity class (which I am setting as DataContext property) I have "Color" property defined as following
So if I type any arbitary value in the combo, it's clearing it up and also throwing the BindingValidationError, but the ComboBox isn't showing up the Error. Also Other controls(mostly TextBox) are showing up the validation error message.
To show the validation error I am using Tooltip and the code is given below.
Any idea what I am doing wrong?
Thanks
Bhaskar
In one of page I have a Combo the XAML is given below.
<telerikInput:RadComboBox IsEditable="True" LostFocus="txtHairColor_LostFocus" ItemTemplate="{StaticResource ComboBoxCustomTemplate}" telerik:TextSearch.TextPath="Code" FilteringMode="Contains" IsTextSearchEnabled="True" x:Name="txtHairColor" Height="24" Width="100" BorderBrush="Gray" BorderThickness="1 0 1 0" VerticalAlignment="Center" validation:ValidationSummary.ShowErrorsInSummary="False" Text ="{Binding Path=Color, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}"> |
Since user can Type-in any value in the combobox, in the LostFocus event, I am checking the text and if it's not from within the list, then I am clearing it. Below is the code for the event
private void txtHairColor_LostFocus(object sender, RoutedEventArgs e) |
{ |
RadComboBox cmb = (RadComboBox)sender; |
if (cmb != null) |
{ |
List<SelectItem> item = (List<SelectItem>)cmb.ItemsSource; |
if (item != null) |
{ |
bool isFound = false; |
item.ForEach(delegate(SelectItem temp) { if (temp.Code == cmb.Text) { isFound = true; return; } }); |
if (!isFound) |
{ |
cmb.Text = ""; |
BindingExpression be = cmb.GetBindingExpression(RadComboBox.TextProperty); |
be.UpdateSource(); |
} |
} |
} |
} |
Where I am binding ItemsSource with List<SelectItem>, SelectItem is a custom class with two property, Code and Description.
And in the Entity class (which I am setting as DataContext property) I have "Color" property defined as following
public string Color |
{ |
get |
{ |
return this._Color |
} |
set |
{ |
if (value.Length == 0) |
{ |
throw new Exception("Please select Hair color."); |
} |
this._Color= value; |
OnPropertyChanged("Color"); |
} |
} |
So if I type any arbitary value in the combo, it's clearing it up and also throwing the BindingValidationError, but the ComboBox isn't showing up the Error. Also Other controls(mostly TextBox) are showing up the validation error message.
To show the validation error I am using Tooltip and the code is given below.
<ControlTemplate x:Key="ValidationToolTipTemplate" TargetType="ToolTip"> |
<Grid x:Name="Root" Margin="10,-5" RenderTransformOrigin="0,0" Opacity="0"> |
<Grid.RenderTransform> |
<TranslateTransform x:Name="xform" X="-25" /> |
</Grid.RenderTransform> |
<vsm:VisualStateManager.VisualStateGroups> |
<vsm:VisualStateGroup Name="OpenStates"> |
<vsm:VisualStateGroup.Transitions> |
<vsm:VisualTransition GeneratedDuration="0" /> |
<vsm:VisualTransition To="Open" GeneratedDuration="0:0:0.2"> |
<Storyboard> |
<DoubleAnimation Storyboard.TargetName="xform" |
Storyboard.TargetProperty="X" To="0" |
Duration="0:0:0.2"> |
<DoubleAnimation.EasingFunction> |
<BackEase Amplitude=".3" EasingMode="EaseOut" /> |
</DoubleAnimation.EasingFunction> |
</DoubleAnimation> |
<DoubleAnimation Storyboard.TargetName="Root" |
Storyboard.TargetProperty="Opacity" To="1" |
Duration="0:0:0.2" /> |
</Storyboard> |
</vsm:VisualTransition> |
</vsm:VisualStateGroup.Transitions> |
<vsm:VisualState x:Name="Closed"> |
<Storyboard> |
<DoubleAnimation Storyboard.TargetName="Root" |
Storyboard.TargetProperty="Opacity" To="0" Duration="0" /> |
</Storyboard> |
</vsm:VisualState> |
<vsm:VisualState x:Name="Open"> |
<Storyboard> |
<DoubleAnimation Storyboard.TargetName="xform" |
Storyboard.TargetProperty="X" To="0" Duration="0" /> |
<DoubleAnimation Storyboard.TargetName="Root" |
Storyboard.TargetProperty="Opacity" To="1" Duration="0" /> |
</Storyboard> |
</vsm:VisualState> |
</vsm:VisualStateGroup> |
</vsm:VisualStateManager.VisualStateGroups> |
<Border Margin="4,4,-4,-4" Background="#052A2E31" CornerRadius="5" /> |
<Border Margin="3,3,-3,-3" Background="#152A2E31" CornerRadius="4" /> |
<Border Margin="2,2,-2,-2" Background="#252A2E31" CornerRadius="3" /> |
<Border Margin="1,1,-1,-1" Background="#352A2E31" CornerRadius="2" /> |
<Border Background="#FFDC000C" CornerRadius="2" /> |
<Border CornerRadius="2"> |
<TextBlock Foreground="White" Margin="8,4,8,4" MaxWidth="250" |
TextWrapping="Wrap" |
Text="{Binding (Validation.Errors)[0].ErrorContent}" /> |
</Border> |
</Grid> |
</ControlTemplate> |
Any idea what I am doing wrong?
Thanks
Bhaskar