rejy11
rejy11

Reputation: 752

WPF Mouse Binding on Listbox Item Template Inconsistent

I have a listbox which binds to an observable collection of a custom type which displays each item through my data template:

<ListBox x:Name="ListBox" Style="{StaticResource CustomListBox}"  ItemsSource="{Binding HandStats}" Height="410" Width="150" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding HoleCards[0].CardParts.Value}" Margin="2"/>
                            <Image Source="{Binding HoleCards[0].SuitImagePath}" Width="10" Height="10" Margin="2"/>
                            <TextBlock Text="{Binding HoleCards[1].CardParts.Value}" Margin="2"/>
                            <Image Source="{Binding HoleCards[1].SuitImagePath}" Width="10" Height="10" Margin="2"/>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Runs:" Margin="2"/>
                            <TextBlock Text="{Binding NumberOfRuns}" Margin="2"/>
                            <TextBlock Text="Players:" Margin="2"/>
                            <TextBlock Text="{Binding NumberOfPlayers}" Margin="2"/>
                        </StackPanel>
                        <StackPanel.InputBindings>
                            <MouseBinding Gesture="LeftClick" Command="{Binding Path=DataContext.PopulateReport, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                                          CommandParameter="{Binding ElementName=ListBox, Path=SelectedItem}"/>
                        </StackPanel.InputBindings>
                    </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>          
</ListBox>

Image of my listbox: https://i.sstatic.net/TdYP1.jpg

Now ideally when I click anywhere in the stack panel for each item, the item will be selected and the command will be fired. But my problem is that the item selection and the command firing is not working as it should.

The item will only become selected if I click in between the text blocks or in the empty space to the right of the text blocks, but the command will not be fired. The command will only fire once I have firstly selected the item, and then click again onto one of the text blocks or one of the images.

I'm still quite new to WPF so if I'm not making much sense please let me know, thanks in advance :)

Upvotes: 1

Views: 666

Answers (2)

rejy11
rejy11

Reputation: 752

So instead of using input bindings I decided to use a different method. I created a property in my vm and bound this to the selected item of my list. So now every time I select a new item (which works perfectly now) the property is updated in my vm. Simple!

Upvotes: 1

mm8
mm8

Reputation: 169400

Try to set the Background property of the StackPanel to Transparent:

<StackPanel Orientation="Vertical" Background="Transparent">
...

If it doesn't have a Background the click events won't be raised as expected.

Upvotes: 0

Related Questions