The task of installing selectedvalue for combobox in xaml

Here is a snippet of my xaml:

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

Why is this not working? "200" is not selected when I run it. Ideally, I try to execute SelectedValue = "{Binding MyValue}".

+3
source share
2 answers

The selected value in this case is of type ComboBoxItem, and not an integer or string of your choice.

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

in your case, you need to set the SelectedValuePath value to "Content". (200 in your case contain the contents of a ComboBoxItem)

Example:

<ComboBox x:Name="cbo1" Width="100" SelectedValue="200" SelectedValuePath="Content">
    <ComboBoxItem Name="n1">100</ComboBoxItem>
    <ComboBoxItem Name="n2">200</ComboBoxItem>
</ComboBox>
+4
source
 <ComboBox x:Name="cbo1" Width="100" >
       <ComboBoxItem Name="n1">100</ComboBoxItem>
       <ComboBoxItem Name="n2" IsSelected="True">200</ComboBoxItem>
 </ComboBox>
0
source

Source: https://habr.com/ru/post/1795308/


All Articles