Reputation: 693
I have myself a ListBox in XAML (also my language us C# and I'm using VS2010). In this ListBox I have several items and I also have a SelectionChanged event. The problem for me is that the event is only firing for a part of the item.
This picture will make it clear I think:
In this case, 'n1' is selected, but in order to select a different one, I have to click in a narrow region around the edge of the item - the area that we can now see as dark blue around n1.
Is there a way to apply this SelectionChanged to different parts of the item? Obviously there is text input there, so not that part, but the rest would be great.
The item itself is inside a ListBox control and the data template within ListBox.Item template is as follows:
<Thumb Name="myThumb" Template="{StaticResource NodeVisualTemplate}">
The mentioned static resource is as follows:
<ControlTemplate x:Key="NodeVisualTemplate">
<Border BorderThickness="2" BorderBrush="LightBlue" Margin="2" CornerRadius="5,5,5,5">
<StackPanel>
<TextBlock Text="Test" Background="AntiqueWhite"/>
<TextBlock Text="{Binding Path=NodeText}" Background="Aqua"/>
<StackPanel Orientation="Horizontal">
<TextBox Text="Type here" MinWidth="50"/>
<Image Source="{StaticResource ImgFolder}" Margin="0,0,5,0" Width="32" Height="32"/>
</StackPanel>
</StackPanel>
</Border>
</ControlTemplate>
Upvotes: 0
Views: 1081
Reputation: 169150
You could handle the PreviewMouseLeftButtonDown
of the ListBoxItem
container to select it when you click anywhere inside it:
<ListBox x:Name="lb">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<EventSetter Event="PreviewMouseLeftButtonDown" Handler="lb_PreviewMouseLeftButtonDown" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Thumb Name="myThumb" Template="{StaticResource NodeVisualTemplate}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
private void lb_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
ListBoxItem lbi = sender as ListBoxItem;
lb.SelectedItem = lbi.DataContext;
}
This should fire the SelectionChanged
event as expected.
Upvotes: 2