Set SelectedItem of WPF ComboBox

后端 未结 4 1391
遇见更好的自我
遇见更好的自我 2020-12-09 07:30

   
   

        
相关标签:
4条回答
  • 2020-12-09 08:20

    In this case, you should be able to simply use .Text() to set it:

    cmbBudgetYear.Text = "2010";
    

    For getting the value after a change, though, and maybe it's because I didn't set SelectedValuePath="Content" everywhere, or maybe because I didn't use SelectedValue to set it (and why I'm mentioning it), it becomes slightly more complicated to determine the actual value, as you have to do this after adding the event handler for SelectionChanged in the XAML:

    private void cmbBudgetYear_SelectionChanged(object sender, EventArgs e)
    {
        ComboBox cbx = (ComboBox)sender;
        string yourValue = String.Empty;
        if (cbx.SelectedValue == null)
            yourValue = cbx.SelectionBoxItem.ToString();
        else
           yourValue = cboParser(cbx.SelectedValue.ToString());
    }
    

    Where a parser is needed because .SelectedValue.ToString() will give you something like System.Windows.Controls.Control: 2010, so you have to parse it out to get the value:

    private static string cboParser(string controlString)
    {
       if (controlString.Contains(':'))
       {
           controlString = controlString.Split(':')[1].TrimStart(' ');
       }
       return controlString;
    }
    

    At least, this is what I ran into.... I know this question was about setting the box, but can't address only setting without talking about how to get it, later, too, as how you set it will determine how you get it if it is changed.

    0 讨论(0)
  • 2020-12-09 08:21

    There exist many ways to do this but for your example, I would change the ComboBox-Tag as follows:

    <ComboBox Grid.Row="1" Grid.Column="0" 
              Name="cmbBudgetYear" SelectedValuePath="Content">
    

    I added the attribute-defition SelectedValuePath="Content". After that you can set the value with a corresponding string, e.g.:

    cmbBudgetYear.SelectedValue = "2009";
    

    Take care that the value must be a string. For your example, use

    cmbBudgetYear.SelectedValue = DateTime.Now.Year.ToString();
    

    An additional idea

    If you use the code-behind anyway, would it be a possibility to fill the combobox with integers. Someting like:

    for(int y=DateTime.Now.Year;y>DateTime.Now.Year-10;y--){
     cmbBudgetYear.Items.Add(y);
    }
    

    ..then you can select the values extremly simple like

    cmbBudgetYear.SelectedValue = 2009;
    

    ... and you would have also other advantages.

    0 讨论(0)
  • 2020-12-09 08:23

    In my case I added the values manually with:

    myComboBox.Items.Add("MyItem");
    

    and then I select the wanted one with:

    myComboBox.SelectedItem = "WantedItem";
    

    instead of:

    myComboBox.SelectedValue = "WantedItem";
    
    0 讨论(0)
  • It works fine for me.

    ObservableCollection<OrganizationView> Organizations { get; set; }
    
    Organizations = GetOrganizations();
    
    await Dispatcher.BeginInvoke((Action)(() =>
        {
        var allOrganizationItem = new OrganizationView() { ID = 0, IsEnabled = true, Name = "(All)" }; // It is a class
        Organizations.Add(allOrganizationItem);
        cbOrganizations.DisplayMemberPath = "Name";
        cbOrganizations.SelectedValuePath = "ID";
        cbOrganizations.ItemsSource = null;
        cbOrganizations.ItemsSource = Organizations; // Set data source which has all items
        cbOrganizations.SelectedItem = allOrganizationItem; // It will make it as a selected item
        }));   
    
    0 讨论(0)
提交回复
热议问题