3

I'd like to reset a combobox after every selection is made, to a default text value. This question was asked very well here but that solution didn't work for me at all. A solution that did make sense to me is to set SelectedIndex to -1 and reset Text as shown below

MainWindow.xaml

<ComboBox Name="combobox" SelectionChanged="ComboBox_SelectionChanged" IsEditable="True" IsReadOnly="True" Text="My Default Text">
    <ComboBox.ItemContainerStyle>
        <Style TargetType="ComboBoxItem">
            <Setter Property="Background" Value="Blue"/>
            <Setter Property="BorderBrush" Value="Blue"/>
            <Setter Property="BorderThickness" Value="0"/>
         </Style>
     </ComboBox.ItemContainerStyle>
     <ComboBoxItem Name="selection0">selection0</ComboBoxItem>
     <ComboBoxItem Name="selection1">selection1</ComboBoxItem>
     <ComboBoxItem Name="selection2">selection2</ComboBoxItem>
     <ComboBoxItem Name="selection3">selection3</ComboBoxItem>
     <ComboBoxItem Name="selection4">selection4</ComboBoxItem>
</ComboBox>

MainWindow.xaml.cs

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        string name = selectedItem.Name;
        if (selectedItem != null)
        {
            MessageBox.Show(string.Format(string));

            //This does set the combobox to empty, but no text is added.
            this.combobox.SelectedIndex = -1;
            this.combobox.Text = "My Default Text";
        }
     }

The SelectedIndex does successfully go to -1, but it stays empty. I'd like the text to go back to what it originally says, but I've had no luck. Any help is appreciated.

Community
  • 1
  • 1
trueCamelType
  • 2,198
  • 5
  • 39
  • 76
  • Why you need do this? :) – Yuri Dorokhov Feb 19 '15 at 00:08
  • 1
    This specific thing, I wouldn't want to do necessarily, but I have a larger problem I'm trying to solve, and the answer to this question would logically solve my larger problem. But why would someone do this is a valid question :) – trueCamelType Feb 19 '15 at 00:09

2 Answers2

3

Once you've obtained the selected item, you can reset the ComboBox back to its default state, but you must do so in a separate Dispatcher message:

private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (this.combobox.SelectedItem != null)
    {
        MessageBox.Show(this.combobox.SelectedItem.ToString());
    }

    Action a = () => this.combobox.Text = "My Default Text";
    Dispatcher.BeginInvoke(a);
}

If you try to do it in the same message then your changes are effectively superseded by WPF's internal logic that runs once your event handler has completed.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
0

I use the DropDownOpened event in code behind to reset the ComboBox every time it is opened.

    public partial class ChangeArticleAndSoftwareBundleRevisionDialog : UserControl
    {
        public Dialog()
        {
             InitializeComponent();

             ComboBox.DropDownOpened += ComboBox_DropDownOpened;
        }

        private void ComboBox_DropDownOpened(object sender, System.EventArgs e)
        {
              ComboBox combobox = (sender as ComboBox);

              combobox.SelectedIndex = 0;
              combobox.SelectedValue = null;
              combobox.SelectedItem = null;
        }
    }
Michael Santos
  • 466
  • 7
  • 15