且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 xaml 中为组合框设置 selectedvalue 时出现问题

更新时间:2023-12-06 17:28:10

本例中选择的值来自 ComboBoxItem 类型,而不是您希望的整数或字符串.

The selected value in this case is from type ComboBoxItem and not integer or string as you wished.

那你能做些什么呢?组合框存在一个属性,它定义了所选对象的哪个属性应该用作值,哪个属性用作 DisplayMember(可视化)

so what can you do against that? there exists a property for the combobox which defines which property of the selected object should be used as value and which as DisplayMember (the visualization)

在您的情况下,您必须将 SelectedValuePath 设置为内容".(在您的情况下,200 是 ComboBoxItem 的内容)

in your case you have to set the SelectedValuePath to "Content". (The 200 are in your case the content of the ComboBoxItem)

示例:

<ComboBox x:Name="cbo1" Width="100" SelectedValue="200" SelectedValuePath="Content">
    <ComboBoxItem Name="n1">100</ComboBoxItem>
    <ComboBoxItem Name="n2">200</ComboBoxItem>
</ComboBox>