Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a combobox from selecting an item from its list in wpf?

Tags:

combobox

wpf

This combobox should display a bound value as its text, and cannot be disabled. I just want to prevent a user from changing the displayed value.

like image 972
Peter Avatar asked Oct 13 '09 11:10

Peter


People also ask

Which event can be used to detect changes in list ComboBox selection?

ComboBox. SelectedIndexChanged Event (System.

How do I disable my combo box?

We can Enable or Disable the options in the given Combobox widget by providing the state property. The state property forces to make a widget either active or disabled. To disable the Combobox widget, we have to set the state property as readonly or disabled.

How do I add a ComboBox item in WPF?

On button click event handler, we add the content of TextBox to the ComboBox by calling ComboBox. Items. Add method. Now if you enter text in the TextBox and click Add Item button, it will add contents of the TextBox to the ComboBox.


2 Answers

    comboBoxName.IsHitTestVisible = false;
    comboBoxName.Focusable = false;

Use these two line codes together.

Edit Note: Edited to solve the problem described by skypecakes

like image 147
Towhid Avatar answered Sep 27 '22 02:09

Towhid


Accoring to MSDN you need

IsReadOnly = true;
IsEditable = false;

See http://msdn.microsoft.com/en-us/library/system.windows.controls.combobox.isreadonly.aspx under Remarks

Edit: actually, I'm not sure any more

I suggest using a style to set ReadOnly to true on PART_EditableTextBox

like image 20
Chris Bednarski Avatar answered Sep 23 '22 02:09

Chris Bednarski