0

I have a combobox with the following options: "HardDelete", "SoftDelete", "MoveToDeletedItems"

I want the default selection to match with the following property of an EmailAction object:

public DeleteMode DeleteMode { get; set; }

Here is the line of code I'm using to try to set this:

cmboDelMode.SelectedItem = emailActionInstance.DeleteMode.ToString();

Related XAML:

<ComboBox x:Name="cmboDelMode" HorizontalAlignment="Left" Margin="149,218,0,0" VerticalAlignment="Top" Width="120">
    <ComboBoxItem Content="HardDelete" HorizontalAlignment="Left" Width="118"/>
    <ComboBoxItem Content="SoftDelete" HorizontalAlignment="Left" Width="118"/>
    <ComboBoxItem Content="MoveToDeletedItems" HorizontalAlignment="Left" Width="118"/>
</ComboBox>

Currently the combobox is defaulting to being empty so is not working as expected. I am able to use "emailActionInstance.DeleteMode.ToString();" to view the data in a text box, so it seems I might just be setting the selected item incorrectly?

1 Answer 1

1

The issue is that setting ComboBox.SelectedItem doesn't work unless the ComboBox contains the item you are setting it to. In your case your ComboBox is filled with three ComboBoxItem objects which have their Content property set to a string. So ComboBox.SelectedItem is a ComboBoxItem. You are trying to set the ComboBox.SelectedItem to a string, which will not equal any of the objects contained in the ComboBox. Therefore, nothing happens.

I would suggest setting up Binding for your ComboBox like here:

<ComboBox ItemsSource="{Binding DeleteModes}" SelectedItem="{Binding SelectedDeleteMode}"  />

And then create a ViewModel to bind to. If you bind an Enum to a ComboBox it will display correctly so you won't need to call DeleteMode.ToString():

public class ViewModel : INotifyPropertyChanged
{
    public ViewModel()
    {
        DeleteModes = new ObservableCollection<DeleteMode>
        { DeleteMode.HardDelete, DeleteMode.SoftDelete,
          DeleteMode.MoveToDeletedItems };
    }

    public event PropertyChangedEventHandler PropertyChanged;

    DeleteMode _selected_delete_mode;
    public DeleteMode SelectedDeleteMode {
        get { return _selected_delete_mode; }
        set {
            _selected_delete_mode = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedDeleteMode"));
        }
    }

    ObservableCollection<DeleteMode> _delete_modes;
    public ObservableCollection<DeleteMode> DeleteModes {
        get { return _delete_modes; }
        set {
            _delete_modes = value;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("DeleteModes"));
        }
    }

    public void update_mode(DeleteMode mode) => SelectedDeleteMode = mode;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.