使用Tabcontrol WPF时无法在列表框中选择项目 [英] Cant select Items in Listbox when using Tabcontrol WPF

查看:73
本文介绍了使用Tabcontrol WPF时无法在列表框中选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当列表框位于Tab控件中时,我在选择列表框中的项目时遇到问题. 我无法在列表框中选择任何项目. 我正在通过代码隐藏来动态填充列表框,但是我也在其上使用了拖放功能,不过,拖放功能正在与tabcontrol一起使用.

I have a problem with the selection of items in a Listbox, when the Listbox is in a Tabcontrol. I can't select any item in the Listbox. I am filling the Listbox dynamically via code-behind, also I am using drag and drop on it, though, Drag and drop is working with the tabcontrol.

这是我的XAML代码:

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>

如果我删除Tabcontrol,一切正常. 我希望有人可以帮助我或知道问题出在哪里.

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

打招呼

推荐答案

我发现了问题. 问题是Microsoft如何设计MessageHandles. 如果某项的子项引发一条消息(例如selectionChanged),但该消息没有处理,则该消息将转到父​​项. 因此,就我而言,如果我单击列表框中的一个项目,则(未处理的)消息"selectionChanged"已发送到TabControl,这就是问题所在.因为我在TabControl.selectionChanged中有自定义代码,所以它总是运行我的代码,而不是在ListBox中选择该项.

I have found the problem. The problem is how Microsoft designed the MessageHandles. If a child of an item throws a message (for example selectionChanged) and the message is not handles, the message goes to the parent Item. So, in my case, if I click on an item in the ListBox, the (unhandled) message "selectionChanged" was sent to the TabControl, this was the problem. Because i have custom code in the TabControl.selectionChanged it always ran my code, instead of selecting the item in the ListBox.

解决方法是,将此代码放入ListBox的selectionChanged事件处理程序中:

The workaround is, to put this code in the selectionChanged eventhandler of the ListBox:

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

这避免了将消息从子消息处理程序转移到父消息处理程序的情况.

This avoids the transfer of the message from the child messagehandler to the parent messagehandler.

我希望你能理解我的解释.

I hope u can undersand my explanation.

这篇关于使用Tabcontrol WPF时无法在列表框中选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆