Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting a Default value in XAML in combobox

Tags:

c#

wpf

xaml

I'm trying to set a default value for a combobox. I have scripts that create the code tables, so writing the code like this is not an option:

<StackPanel>
<ComboBox SelectedValue="CA">
    <ComboBoxItem Tag="CO">Colorado</ComboBoxItem>
    <ComboBoxItem Tag="CA">California</ComboBoxItem>
    <ComboBoxItem Tag="NM">New Mexico</ComboBoxItem>
</ComboBox>
</StackPanel>

Instead I have some code in the XAML:

 <StackPanel HorizontalAlignment="Left" Margin="10,5,0,0" Name="PersonStackPanel" Height="60">
            <Label VerticalContentAlignment="Top" FontWeight="Normal" Height="28" HorizontalAlignment="Left">Person</Label>
            <ComboBox Name="PersonComboBox"  Width="312" Props.CodeProvider="MASTCODE.TYPE.ARRA" DisplayMemberPath="NAME" Tag="AA" IsSelected="True" SelectedValuePath="CODE" SelectedItem="{Binding Path=PERSON}"/>
 <StackPanel>

i thought Tag="AA" IsSelected="True" would work but it is not.

like image 944
Bobby Ricky Avatar asked Jan 09 '14 15:01

Bobby Ricky


2 Answers

Any value that is displayed in a combobox has to be present as a selection.

Try:

<ComboBox SelectedIndex="0"/>    

Edit: added quotations

like image 96
Gusdor Avatar answered Nov 02 '22 18:11

Gusdor


You are using

SelectedValuePath="CODE"

so in this case try to set SelectedValue instead of SelectedItem

like image 31
Lukas Kubis Avatar answered Nov 02 '22 18:11

Lukas Kubis