Usha T

Usha T

  • NA
  • 44
  • 5.2k

validate combo on button click in wpf mvvm

May 26 2015 2:25 AM

I am creating user control , in which I have placed combo, binded with Enumerations. Binding this enum list has no error.

I try to validate selectedvalue of this combo in button click. But this throw's error while page loads as ,
Value " could not be converted.

If I select 1st option (none) from the list , this error message disapears.
I think, selectedvalue property takes empty string in some where, I can't traceout this.

My coding is as below

<ComboBox x:Name="cbLivingRoomFloorType" Grid.Row="3" Grid.Column="2" BorderBrush="#B6B6B4" BorderThickness="2" ItemsSource="{Binding Path=LivingRoomFloorTypeList}" SelectedValuePath="Value" DisplayMemberPath="Description" SelectedValue="{Binding SelectedLivingRoomType,ValidatesOnDataErrors=True,UpdateSourceTrigger=PropertyChanged}" Margin="0,2"/>
<ContentPresenter x:Name="cpLivingRoom" Grid.Row="4" Grid.Column="2" Content="{Binding Path=(Validation.Errors)[0],ElementName=cbLivingRoomFloorType}">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<TextBlock Foreground="Red" Text="{Binding Path=ErrorContent}" />
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
 
In viewmodel
#region Living room
public IEnumerable<ValueDescription> LivingRoomFloorTypeList
{
get
{
return Utility.GetAllValuesAndDescriptions<EnumAptitudeOfLivingRoomFloor>();
}
}
private EnumAptitudeOfLivingRoomFloor _livingRoomFloorType;
public EnumAptitudeOfLivingRoomFloor SelectedLivingRoomType
{
get
{
return _livingRoomFloorType;
}
set
{
if (_livingRoomFloorType == value)
return;
_livingRoomFloorType = value;
RaisePropertyChanged("SelectedLivingRoomType");
}
}
#endregion

I can't trace out the issue. Help me on this.

Thanks in advance

 
 
 

Answers (1)