Ben Bodie
Ben Bodie

Reputation: 75

C# Update Combobox text after SelectedItemChange

I have a combobox that i would like the physically displayed text to always remain the same.

I want the user to select an item which would then get passed in but then for the actual text on the combobox to remain the same.

on the

FileBox_SelectionChanged(object sender, SelectionChangedEventArgs e)

event i find out which item has been selected with

 if (((ComboBox)sender).SelectedItem != null)
        {
            if (((ComboBox)sender).SelectedItem.ToString() == "New File")
            {

            }
        }

(i will process this more later)

I then try and update the text back to being 'File'.

I have tried numerous approaches which dont seem to work.

I've tried simply doing

FileBox.text = "File";

this.Dispatcher.Invoke(() =>
        {
            FileBox.Text = "File";
        });

FileBox.SelectedItem = "File";

When debugging it does actually seem like the .Text property gets updated, but it then seems to get overridden when the event finishes. For testing i have a button that does:

var text = FileBox.Text;
FileBox.Text = "File";

When i have selected "New File" the var text == New File

And the FileBox.Text code here works and updates it back to File

Do i need to set the text again outside of the SelectionChanged event, and if so how would i go about doing this?

Thanks

EDIT

I do not think this is a duplicate of the one posted as he wants his default to dissapear when something is selected, i want it to reappear

Upvotes: 0

Views: 563

Answers (1)

Mac
Mac

Reputation: 957

The approach is actually not ideal, you should use MVVM pattern, but this is my answer to your question, hope it helps.

<ComboBox x:Name="FileBox"
                  SelectedIndex="0"
                  SelectionChanged="FileBox_OnSelectionChanged"
                  Width="180" Height="50" > 

Code-behind

private void FileBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var fileBox = sender as ComboBox;

            if (fileBox != null)
            {
                var selectedItem = fileBox.SelectedItem;

                // get the selected item.
                Debug.WriteLine(selectedItem);

                fileBox.SelectionChanged -= FileBox_OnSelectionChanged;
                fileBox.SelectedIndex = 0;
                fileBox.SelectionChanged += FileBox_OnSelectionChanged;
            }
        }

Assuming that this is how you populate your control:

private void PopulateFileData()
        {
            FileDataList = new List<FileData>
            {
                new FileData{ FileName = "Files", Path = "" },
                new FileData{ FileName = "File 123", Path = @"c:\file1.txt" },
                new FileData{ FileName = "File 456", Path = @"c:\file2.txt" }
            };
        }

        private void FillComboBox()
        {
            foreach (FileData file in FileDataList)
            {
                FileBox.Items.Add(file.FileName);
            }
        }

Check your output window.

Upvotes: 1

Related Questions