talal shoukat
talal shoukat

Reputation: 89

Change SelectedValuePath on runtime

I have a combobox which is bound to a collection of entity with properties like number & name. Intially I have set my combobox SelectedValuePath to NAME.

<my1:ComboBox Name="LHScmb" 
              Width="Auto" 
              Height="Auto" 
              Grid.Row="0" 
              Grid.Column="0" 
              IsEditable="False" 
              DisplayMemberPath="VARNAME" 
              SelectedValuePath="NAME"
              ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}},Path=DataContext.BRPARMINPT}"
              SelectedValue="{Binding LHSITEMSRCECDE, UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}"   
              SelectionChanged="cmbLHS_SelectionChanged"/>

In some case when name is null I change SelectedValuePath of my combobox to number. As shown in sample code below

if (item.NAME == null)
{
   LHScmb.SelectedValuePath = "NUMBER";
}

But this gives an exception Input string was not in a correct format.

Is there any other way to set SelectedValuePath on runtime.

Upvotes: 0

Views: 93

Answers (1)

Fratyx
Fratyx

Reputation: 5797

I suppose your problem is not setting SelectedValuePath but that your NUMBER property is numeric. So if there is a string in the combobox which can't be interpreted as a number and suddenly you try to bind it to your numeric property, you will get this error.

Upvotes: 2

Related Questions