Cannot select items in a list when using Tabcontrol WPF

I have a problem with selecting items in a Listbox when the Listbox is in Tabcontrol. I can not select any item in the list. I dynamically populate the Listbox with code, I also use drag and drop on it, but Drag and drop works with tabcontrol.

Here is my XAML code:

<Window x:Class="SPInstallApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:toolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit.Extended" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="SharePoint 2010 - wspSync" Height="450" Width="700" AllowDrop="True" Icon="/SPInstallApp;component/Images/favicon.ico"> <Window.Resources> <DataTemplate x:Key="CustomListBoxTemplate"> <StackPanel> <Grid Margin="4"> <Grid.ColumnDefinitions> <ColumnDefinition Width="48 "/> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Image Source="{Binding Path=ImageSource}" Grid.Column="0" Grid.RowSpan="3" Margin="0,0,5,0" /> <TextBlock Padding="0,5,0,0" Text="{Binding Path=Title}" Grid.Column="1" Grid.Row="0" FontWeight="Bold"/> <TextBlock Padding="0,0,0,5" Text="{Binding Path=Description}" Grid.Column="1" Grid.Row="1" FontStyle="Italic" /> <TextBlock Padding="0,0,0,5" Text="{Binding Path=Status}" Grid.Column="1" Grid.Row="2" FontStyle="Italic" Foreground="#FFDE2B2B" /> </Grid> </StackPanel> </DataTemplate> </Window.Resources> <toolkit:BusyIndicator IsBusy="True" BusyContent="Bitte warten..." Name="busyIndicator"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="*"/> <RowDefinition Height="50"/> </Grid.RowDefinitions> <Label Content="Websitecollection wΓ€hlen:" Grid.Row="0" Grid.Column="0" Margin="5,0,0,0" /> <ComboBox Grid.Row="1" Grid.Column="0" Height="20" Margin="10,0,10,10" Name="cbWebsitecollection" SelectionChanged="CbWebsitecollectionSelectionChanged" /> <TabControl Grid.Row="2" Grid.Column="0" Name="tc" SelectionChanged="TcSelectionChanged" Margin="10,0,10,0"> <TabItem Header="Installieren"> <ListBox AllowDrop="True" Background="#CCC" Drop="ListBoxDrop" Name="lbDropbox" IsSynchronizedWithCurrentItem="True" ItemTemplate="{StaticResource CustomListBoxTemplate}" KeyUp="LbDropboxKeyUp" /> </TabItem> <TabItem Header="Websitecollection"> <CheckBox Content="test" /> </TabItem> </TabControl> <Label Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" FontWeight="Bold" Content="drag 'n' drop" Margin="10" Drop="ListBoxDrop" Name="lbDescription" /> <Button Grid.Row="3" Grid.Column="0" Name="cmdSync" Content="Synchronisieren" Margin="10" Width="100" HorizontalAlignment="Right" Click="CmdSyncClick" /> <Image Grid.Row="3" HorizontalAlignment="Left" Name="Logo" Source="/SPInstallApp;component/Images/logo.gif" Margin="10" MouseUp="LogoMouseUp" MouseEnter="LogoMouseEnter" MouseLeave="LogoMouseLeave" /> </Grid> </toolkit:BusyIndicator></Window> 

If I remove Tabcontrol, everything will work. I hope someone can help me or know what the problem is.

greets

+2
source share
1 answer

I found a problem. The problem is how Microsoft developed MessageHandles. If a child sends a message (e.g. selectionChanged) and the message is not processed, the message goes to the parent. So, in my case, if I click on an item in a ListBox, the (raw) message "selectionChanged" was sent to TabControl, it was a problem. Since I have custom code in TabControl.selectionChanged, it always ran my code, rather than selecting an item in a ListBox.

The workaround is to put this code in the eventChanged ListBox handler:

 private void ListBox_selectionChanged(object sender, DragEventArgs e) { e.handled = true; } 

This avoids sending the message from the child message handler to the parent message handler.

I hope you can have my explanation.

+2
source

Source: https://habr.com/ru/post/1499954/


All Articles