none
Making certain combobox items non-selectable?

Answers

  • Set IsEnabled = False on the ListBoxItem and you will not be able to select it. By default this changes the appearance of the item, but you can control this by overring the template.

     

    HTH,
    Drew

    Thursday, June 14, 2007 8:49 PM
  • But in this situation how would you tell if a certain item was checked?  I'm asking because I've run into this type of thing before and didn't know the answer...
    Monday, June 18, 2007 3:46 PM

All replies

  • Set IsEnabled = False on the ListBoxItem and you will not be able to select it. By default this changes the appearance of the item, but you can control this by overring the template.

     

    HTH,
    Drew

    Thursday, June 14, 2007 8:49 PM
  • I forgot to add details about my situation.  In my case the combobox item is a checkbox.  So if the combobox item is a check box and I want the user to be able to check and uncheck it, it has to be enabled for this to happen...
    Thursday, June 14, 2007 9:00 PM
  • So are you just trying to achieve selection with checkboxes? Meaning all you really want to is indicate an item is selected with a checkbox and not show the selection highlight?

     

    If so, this is much easier and can be done just with templating/databinding. You can just remove the property triggers that do the highlighting and databind the checkbox's IsChecked property to the ListViewItem's IsSelected property using a RelativeSource with AncestorType ListBoxItem:

     

    IsChecked="{Binding RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}, Path=IsChecked}"

     

    HTH,
    Drew

    Thursday, June 14, 2007 9:44 PM
  • Basically all I want is to have a combobox with checkboxes as items that someone can check or uncheck while the combobox is expanded, but I don't want any of the check box items to be selectable (in terms of a selectable combobox item).
    Thursday, June 14, 2007 9:47 PM
  • Right, but the whole point of checking the checkbox is so that you can figure out which ones are selected by using ListBox's SelectedItems property right? If you do what I proposed above it will enabled exactly that scenario.

     

    HTH,
    Drew

    Thursday, June 14, 2007 10:45 PM
  • This is a comboboxitem, not listboxitem (see above... my first post).  I guess you're saying put a listbox as one of my combobox items...?
    Thursday, June 14, 2007 11:49 PM
  • I'm a little confused why you would even have checkbox selection in a ComboBox in the first place since it's usually for selecting a single item and displaying it, but forgetting about that and databinding for a second... is this is or is this not the behavior you're looking for: 

    Code Snippet

    <ComboBox IsSynchronizedWithCurrentItem="True">
       <ComboBox.Resources>
        <Style TargetType="{x:Type ComboBoxItem}">
         <Setter Property="Template">
          <Setter.Value>
           <ControlTemplate TargetType="{x:Type ComboBoxItem}">
            <CheckBox Width="Auto" Height="Auto" Content="{TemplateBinding Content}"/>
           </ControlTemplate>
          </Setter.Value>
         </Setter>
        </Style>
       </ComboBox.Resources>
       <ComboBoxItem Content="Item 1"/>
       <ComboBoxItem Content="Item 2"/>
       <ComboBoxItem Content="Item 3"/>
      </ComboBox>

     

    Lemme know,

    Drew

    Friday, June 15, 2007 7:08 PM
  • Yes, that's the correct behavior, thanks.  I know this question was a bit odd, but I guess that's because I'm stubborn Wink.  I apologize.  As far as why you would have a checkbox selection in a combobox, think about if you had a bunch of data records and you wanted to filter on a column-by-column basis.  Maybe one column is zodiac sign which can be 1 of 12 things.  The drop down for that column would contain 12 check boxes so I could show JUST the Aquarius, or JUST the Sagitarius AND Capricorn, or any combination.  Excel has this feature with its filter menus.  Anyway, thanks for your help Drew.
    Monday, June 18, 2007 2:37 AM
  • In your code, how would I be able to actually tell if an item was checked?  The checkbox is defined in the style...
    Monday, June 18, 2007 2:35 PM
  • that's handled as part of ComboBox's SelectedItems property. That the checkbox is checked or not is irrelevant, it's purely a UI implementation
    Monday, June 18, 2007 3:06 PM
  • But in this situation how would you tell if a certain item was checked?  I'm asking because I've run into this type of thing before and didn't know the answer...
    Monday, June 18, 2007 3:46 PM
  • You would know it is checked because, as Kayakyakr pointed out, it would appear in the SelectedItems collection of the ComboBox.

     

    HTH,
    Drew

    Monday, June 18, 2007 3:54 PM