2

I have a ListBox for a few items, and I need to be able to click them. Problem is, the SelectionChanged event doesn't get fired when I click on the item's text, only if I click on the blank part. I'm quite new to WPF, and I don't understand why this is happening.

XAML:

<ListBox Name="lBoxVouchers" BorderThickness="0" FontSize="15" SelectionChanged="lBoxVouchers_SelectionChanged">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ListBoxItem Content="{Binding Name}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Handler:

private void lBoxVouchers_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (e.AddedItems.Count > 0)
        MessageBox.Show("You just selected " + e.AddedItems[0]);
}

I'm binding the list of objects in code via the lBoxVouchers.ItemsSource property, and they show up. Each object has a Name property, of course.

I've tried setting IsEnabled on the ListBox and the items, both in code and XAML, but it doesn't help.

Any comments about better ways to do this in WPF are also welcome.

3 Answers 3

2

If you only want to show the Name property you could define your listbox like this:

<ListBox Name="lBoxVouchers" BorderThickness="0" FontSize="15" SelectionChanged="lBoxVouchers_SelectionChanged" DisplayMemberPath="Name" />

If you put your items on an ObservableCollection on code-behind, you can also pass the databinding to XAML:

<ListBox Name="lBoxVouchers" BorderThickness="0" FontSize="15" SelectionChanged="lBoxVouchers_SelectionChanged" DisplayMemberPath="Name" ItemsSource={Binding Path=Items}" />

And on your code behind you should have something like:

ObservableCollection<object> Items {get; set}

About the handler, I would also do something like this:

private void lBoxVouchers_SelectionChanged(object sender, SelectionChangedEventArgs e) {
    if (((ListBox)sender).SelectedItem != null)
        MessageBox.Show("You just selected " + (ListBox)sender).SelectedItem);
}
Sign up to request clarification or add additional context in comments.

5 Comments

He already said that he managed to make the data binding and tthe items show up. You did not answer the question just shown a different way to realize the same goal.
Which is good because I said I'd appreciate any helpful comments about my code.
Using the DisplayMemberPath property fixed it. Still have no idea why it didn't work the other way.
I think that Ed previous code wasn't working because he was redefining the listBox DataTemplate and probably overriding some events. I just show him the way I normally implement this kind of problem
A better way of achieving this is explained over here, social.msdn.microsoft.com/Forums/en/wpf/thread/…
0

Set IsSynchronizedWithCurrentItem="true" on the listbox.

Here you can find a starting point to get more details about this property.

Setting this property to true makes the selection be in sync with the current item which holds the actual selected item. When you click the blank space probably the current item changes to null and you get your event handler called.

Comments

0

may be the content in the listbox item is not stretched. just write this style for the listbox item and try.

<Style TargetType="{x:Type ListBoxItem}">
  <Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.