views:

506

answers:

2

I would like to prevent selection of ListBoxItems in my ListBox. My DataTemplate has a checkbox and this should be the only thing the user can click or select. How can I do that? Thanks!

A: 

When your user will try to (un)check your checkboxes then item become 'active' in some way. And focused style will be applied. As far as i know there is no way to disable selection(because if you did your checkboxes will not work) but you can override focused(or selected) style of your listbox items

Trickster
Using ItemsCOntrol, like Drew says, does the trick.
Gustavo Cavalcanti
+4  A: 

This is almost a duplicate question. In fact, you're asking two questions here:

  1. Either style your ListBoxItem so that it doesn't show selection (look elsewhere on SO for that answer), or replace ListBox with ItemsControl if you don't need the other features that ListBox provides.

  2. Bind your checkbox's IsChecked property to the parent ListBoxItem.IsSelected property:

    <CheckBox 
       IsChecked="{Binding
          RelativeSource={RelativeSource Mode=FindAncestor, 
                                         AncestorType=ListBoxItem},
          Path=IsSelected}"
    />
    
Drew Noakes