tags:

views:

335

answers:

2

I have a ListBox with a rather simple ItemTemplate defined - containing a TextBlock and a Button. This displays like expected, but there is a problem though. When I click the content of the ListBoxItem, i.e. the text or the button, the line doesn't get selected in the ListBox. If I click the blank parts of the line it does. I'd like the ListBoxItem to be selected when I click anywhere on the line. What's needed to achieve this?

<ListBox ItemsSource="{Binding SomeElements}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ListBoxItem Selected="ListBoxItem_Selected">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Title}"></TextBlock>
                    <Button>Click</Button>
                </StackPanel>                                                
            </ListBoxItem>                                            
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
+1  A: 

Here is the answer to your problem.

The questionstarter was experiencing the complete same problem.

Natrium
It should be a simpler solution that creating a custom ListboxItem implementation!
code-zoop
it's the only solution there is, but it works very well.
Natrium
Thx, but is this really the best solution? Is there no builtin properties etc to handle this? I find it odd that Wpf doesn't handle this by default, or at least doesn't provide a simple trigger to enable this - without writing your own custom ListBox. No? This is the way to go?
stiank81
This is the way to go I'm afraid. And you are right: you should think they should have thought this through, but I guess they did not :-) I have the same problem with a TreeView, but this workaround does not work for a treeView, since SelectedItem is readonly. So I don't have a solution for a TreeView yet...
Natrium
Damn.. Well - your custom FocusableListBox control is only a few lines of code anyway, so I can live with that. Will leave the question open for a day to see if anyone else has opinions on this. Thanks!
stiank81
I need to do something more than just implementing the FocusableListBox and using this instead? Route some events etc? Replaced the ListBox with your FocusableListBox, but it doesn't solve my problem. Items still not selected when clicking the text. Also; it crashes when ParentListBox == null (yes, easy to fix..).
stiank81
+3  A: 

@Natrium Nope, the problem is different here,

  1. You need to remove ListBoxItem inside DataTemplate. DataTemplate can not have item ListBoxItem, and it will not work correctly. Whatever you define in DataTemplate is automatically put inside ListBoxItem at runtime, so in your case, this is what it gets created.
ListBoxItem
    DataTemplate
        ListBoxItem
            StackPanel...
  1. If you want to track selection event, then you should only catch ListBox.SelectionChange event, you dont need to track ListBoxItem_Selected.

Change your code to this.

<ListBox ItemsSource="{Binding SomeElements}">
    <ListBox.ItemTemplate>
        <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Title}"></TextBlock>
                    <Button>Click</Button>
                </StackPanel>
         </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>
Akash Kava
Perfect..! Thanks for clarifying this :-)
stiank81