【问题标题】:Cant select Items in Listbox when using Tabcontrol WPF使用 Tabcontrol WPF 时无法选择列表框中的项目
【发布时间】:2011-10-28 11:47:00
【问题描述】:

当列表框位于 Tabcontrol 中时,我在选择列表框中的项目时遇到问题。 我无法选择列表框中的任何项目。 我正在通过代码隐藏动态填充列表框,我也在使用拖放操作,但是,拖放正在使用 tabcontrol。

这是我的 XAML 代码:

<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,一切正常。 我希望有人可以帮助我或知道问题所在。

问候

【问题讨论】:

  • 我试过你的代码,只要忙信号不是“忙”,我从列表中选择任何东西都没有问题。所以我认为问题不在于您在此处发布的 xaml。了解您如何将内容添加到列表中或某些事件处理器中的内容可能会很有用。

标签: c# wpf listbox selection tabcontrol


【解决方案1】:

我发现了问题。 问题在于微软是如何设计 MessageHandles 的。 如果某个项的子项引发消息(例如 selectionChanged)并且该消息没有句柄,则该消息将发送到父项。 因此,就我而言,如果我单击 ListBox 中的某个项目,则(未处理的)消息“selectionChanged”被发送到 TabControl,这就是问题所在。因为我在 TabControl.selectionChanged 中有自定义代码,所以它总是运行我的代码,而不是选择 ListBox 中的项目。

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

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

这避免了消息从子消息处理程序到父消息处理程序的传输。

希望你能理解我的解释。

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 2021-04-11
    • 2016-09-22
    • 1970-01-01
    • 1970-01-01
    • 2011-11-09
    • 2019-07-14
    • 1970-01-01
    相关资源
    最近更新 更多