C# 在运行时更改SelectedValuePath
我有一个combobox,它绑定到一个实体集合,该集合具有数字和名称等属性。最初,我已将组合框SelectedValuePath设置为NAMEC# 在运行时更改SelectedValuePath,c#,wpf,C#,Wpf,我有一个combobox,它绑定到一个实体集合,该集合具有数字和名称等属性。最初,我已将组合框SelectedValuePath设置为NAME <my1:ComboBox Name="LHScmb" Width="Auto" Height="Auto" Grid.Row="0" Grid.Column="0" IsEditable="Fals
<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"/>
但这会导致输入字符串格式不正确的异常
是否有其他方法在运行时设置SelectedValuePath。我想您的问题不是设置SelectedValuePath,而是您的
NUMBER
属性是数字。因此,如果组合框中有一个字符串不能解释为数字,并且突然尝试将其绑定到数值属性,则会出现此错误
if (item.NAME == null)
{
LHScmb.SelectedValuePath = "NUMBER";
}