Problem setting selectedvalue for combobox in xaml

前端 未结 2 1760
难免孤独
难免孤独 2021-01-21 04:51

Here is a snippet of my xaml:

    
        100

        
相关标签:
2条回答
  • 2021-01-21 05:06

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

    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)

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

    example:

    <ComboBox x:Name="cbo1" Width="100" SelectedValue="200" SelectedValuePath="Content">
        <ComboBoxItem Name="n1">100</ComboBoxItem>
        <ComboBoxItem Name="n2">200</ComboBoxItem>
    </ComboBox>
    
    0 讨论(0)
  • 2021-01-21 05:26
     <ComboBox x:Name="cbo1" Width="100" >
           <ComboBoxItem Name="n1">100</ComboBoxItem>
           <ComboBoxItem Name="n2" IsSelected="True">200</ComboBoxItem>
     </ComboBox>
    
    0 讨论(0)
提交回复
热议问题