Reputation: 2092
So, when I use a standard WPF ListBox which displays its items, I can see half of the bottom item on the screen. When I click on this item it brings it into the view, so I can see the whole item. This is perfect; however, I’m using a double click to open a form. If the item I double clicked on is the last one and only part of it is in the view, then the first click brings this item into the view, but the second click selects the next item and opens it. While the intention was to open the item that got the first click. Is there any way around it? Here is the xaml
<ListBox
x:Name="TestListBox"
Loaded="TestListBox_OnLoaded"
MouseDoubleClick="TestListBox_OnMouseDoubleClick">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock FontSize="40" Text="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
And the code
private void TestListBox_OnLoaded(object sender, RoutedEventArgs e)
{
for (var i = 0; i < 50; i++)
{
TestListBox.Items.Add(i);
}
}
private void TestListBox_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
var foo = TestListBox.SelectedItem;
}
If you set a breakpoint to see the value of foo, you will see that double click on an item shows the next item down the list as a selected one. Thank you
Upvotes: 1
Views: 274
Reputation: 4475
Set ScrollViewer.CanContentScroll = "False"
<ListBox
x:Name="TestListBox"
Loaded="TestListBox_OnLoaded"
MouseDoubleClick="TestListBox_OnMouseDoubleClick" ScrollViewer.CanContentScroll = "False">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock FontSize="40" Text="{Binding}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Also you should set your double click event on the items, currently it is assigned to the entire listbox so double clicking the scrollbar for example will trigger the event.
Upvotes: 1