WPF - prevents ListBox items from being selected

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

+2


source to share


2 answers


This is almost a duplicate question. You are actually asking two questions here:



  • Make the style ListBoxItem

    not show highlighting (look elsewhere on SO for this answer) or replace ListBox

    with ItemsControl

    if you don't need other features that ListBox

    .

  • Bind the CheckCheck IsChecked property to the parent property ListBoxItem.IsSelected

    :

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

+7


source


When your user tries (un) to check your checkboxes, then the element will become "active" in some way. And the focused style will be applied. As far as I know, there is no way to disable selection (because if you made your checkboxes it won't work), but you can override the focused (or selected) style of the list items.



0


source







All Articles