最近感觉新的方法Binding comboBox用起来很好用。
记录一下:
<ComboBox Grid.Row="" Grid.Column="" VerticalAlignment="Center" HorizontalAlignment="Left" MinWidth=""
x:Name="cboFamilyName"
DisplayMemberPath="DisplayName"
SelectedValuePath="idRow"
IsEditable="True"
IsReadOnly="True"
ItemsSource="{Binding MyList}"
SelectedValue="{Binding Path=SelectedID, Mode=TwoWay}"
/>
MyList就是某一个Model的list, Model中有idRow和DisplayName
SelectedID表示选中的idRow,放到这个变量中。
选中后,有时候会出现一个讨厌的红色矩形框。去掉矩形框方法(http://stackoverflow.com/questions/4135955/remove-red-rectangle-around-combobox):
Validation.ErrorTemplate="{x:Null}"
About modify the Validation.ErrorTemplate
<ControlTemplate x:Key="ComboBoxValidationErrorTamplate">
<DockPanel>
<Border BorderBrush="Blue" BorderThickness="">
<AdornedElementPlaceholder />
</Border>
</DockPanel>
</ControlTemplate>
And then use it in your ComboBox like
<ComboBoxValidation.ErrorTemplate="{StaticResource ComboBoxValidationErrorTamplate}"
...>