简体   繁体   中英

Setting the initial selection or default value of a combo box

I'm trying to set the initial selected value (or default value) of a combo box. When the user selects an incident record, the incident status from the incident record is supposed to show up as the default selection of the combo box, instead of the blank selection that appears at the top of the combo box list. I've tried:

  1. Two-way binding the combo box selected value to a property in my model and then setting the property as the incident data is loaded,

  2. Two-way binding the combo box selected index to a property in my model and then setting the property as the incident data is loaded,

  3. Two-way binding the both the combo box selected index and value to a properties in my model and then setting the properties as the incident data is loaded,

but no matter what I tried, the combo box still shows as it would initially with the blank line for a default showing on top of the list.

Here is the XAML for the ComboBox:

    <ComboBox  Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2"  
    IsEnabled="{qc:Binding '!$P', P={Binding Path=Form104CModel.IsPartINA}}"
    HorizontalAlignment="Left" 
    DisplayMemberPath="Description" 
    SelectedValuePath="Code" 
    SelectedValue="{Binding Form104CModel.IncidentStatus, Mode=TwoWay}"
    SelectedIndex="{Binding Form104CModel.IncidentStatusSelectedIndex, Mode=TwoWay}"
    Margin="2,0,2,0" Name="cmbxIncidentStatus" 
    FontSize="11" FontWeight="Bold" Width="180">
    <ComboBox.ItemsSource>
        <CompositeCollection>
            <domain:Enumeration Code="" Description="" />
            <CollectionContainer 
                 Collection="{Binding Source={x:Static infraData:CodeCache.ClearedException}}" />
        </CompositeCollection>
    </ComboBox.ItemsSource>
</ComboBox>

Here are the properties in the model to which the selected index and selected value are bound:

  private int incidentStatusSelectedIndex;
    public int IncidentStatusSelectedIndex
   {
        get
        {
            return this.incidentStatusSelectedIndex;
        }

        set
        {
            if (this.incidentStatusSelectedIndex != value)
            {
                this.incidentStatusSelectedIndex = value;
            }
        }
    }

   private string incidentStatus;
    [CustomValidation(typeof(Form104CModel), "ValidateIncidentStatus")]
    public string IncidentStatus
    {
        get
        {
            return this.incidentStatus;
        }

        set
        {
            if (this.incidentStatus != value)
            {
                this.ValidateProperty("IncidentStatus", value);
                this.incidentStatus = value;
                this.RaisePropertyChanged("IncidentStatus");
            }
        }
    }

And here is the code (from the ViewModel) that sets the value of the incident status.

              switch (rr.Incident.IncidentStatus)
                {
                    case "A":
                        this.Form104CModel.IncidentStatus = "CLEARED BY ARREST";
                        this.Form104CModel.IncidentStatusSelectedIndex = 1;
                        break;
                    case "D":
                        this.Form104CModel.IncidentStatus = "DEATH OF OFFENDER";
                        this.Form104CModel.IncidentStatusSelectedIndex = 2;
                        break;
                    case "E":
                        this.Form104CModel.IncidentStatus = "EXTRADITION DECLINED";
                        this.Form104CModel.IncidentStatusSelectedIndex = 3;
                        break;
                    case "J":
                        this.Form104CModel.IncidentStatus = "JUVENILE, NO CUSTODY";
                        this.Form104CModel.IncidentStatusSelectedIndex = 4;
                        break;
                    case "O":
                        this.Form104CModel.IncidentStatus = "OPEN";
                        this.Form104CModel.IncidentStatusSelectedIndex = 5;
                        break;
                    case "P":
                        this.Form104CModel.IncidentStatus = "PROSECUTION DECLINED";
                        this.Form104CModel.IncidentStatusSelectedIndex = 6;
                        break;
                    case "R":
                        this.Form104CModel.IncidentStatus = "REFUSED TO COOPERATE";
                        this.Form104CModel.IncidentStatusSelectedIndex = 7;
                        break;
                    case "U":
                        this.Form104CModel.IncidentStatus = "UNFOUNDED";
                        this.Form104CModel.IncidentStatusSelectedIndex = 8;
                        break;
                }

The only thing that I can think of now is that the ComboxBox.ItemsSource is somehow thwarting my efforts to select a default based on the incident status field.

Any suggestions would be much appreciated.

Not really an answer, but more a series of thoughts too big to fit in comments:

  • Binding both SelectedItem and SelectedValue often leads to problems. SelectedItem is the easier one to deal with, bind it to an observable property on the VM and the combo will react when you set it, assuming you're setting it to an object that exists in the ItemsSource collection. SelectedValue normally needs to be used with SelectedValuePath and is used for deconstructing more complex objects. This SO question gives a good overview of the difference.

  • Which brings me to the ItemsSource . I'm not entirely sure what you're trying to achieve with the CompositeCollection but it doesn't look like it contains anything that would be applicable to the SelectedItem .

At it's most basic, combo box binding is as simple as

<ComboBox ItemsSource="{Binding VMItemsSource}" SelectedItem="{Binding VMSelectedItem}"/>

Where VMItemsSource is a Collection of <Something> and VMSelectedItem is an observable property of the same type:

public ObservableCollection<Something> VMItemsSource {get; set;}

public Something VMSelectedItem
{
  get 
  {
    return this.vmSelectedItem;
  }
  set
  {
    this.vmSelectedItem = value;
    this.RaisePropertyChanged(nameof(this.VMSelectedItem));
  }
}

Then, after the ItemsSource collection is built you just need this to initalise it:

this.VMSelectedItem = this.VMItemsSource.First();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM