You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WPF ListBox底部项点击展开下拉菜单时滚动定位失效

WPF ListBox底部项点击后下拉菜单无法完整显示的问题

我有一个带自定义下拉菜单的WPF ListBox控件,此前已解决过让ListBox项显示在控件外并覆盖其他控件的问题。现在遇到新问题:点击底部项时,无法像上方项那样自动滚动定位,让下拉菜单完整显示。上方项点击时会改变自身尺寸并推动下方项,从而完整显示下拉菜单,但该逻辑在当前自定义ListBox中失效,怀疑与自定义项的实现有关。

自定义项的DataTemplate代码:

<DataTemplate x:Key="MYListBoxItemDataTemplate" DataType="{x:Type models:MyType}">
        <StackPanel x:Name="itemsactions" HorizontalAlignment="Center" Height="135">
            <ToggleButton x:Name="choose" Height="52" Margin="0,27,0,0"
                          IsChecked="{Binding IsSelected, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type ListBoxItem}}}"
                          Command="{Binding DataContext.CurrentViewModel.ClickPalletTypeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                          CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                          Style="{DynamicResource CircledTransparentToggleButtonStyle}">
                <Image x:Name="image" VerticalAlignment="Center" Margin="0,0,0,0" Height="52" Stretch="Uniform">
                    <Image.Source>
                        <BitmapImage UriSource="{Binding ImageSource.Regular}"/>
                    </Image.Source>
                </Image>
            </ToggleButton>
            <StackPanel HorizontalAlignment="Center">
                <Label Margin="0,15,0,0" Width="152" HorizontalAlignment="Center" HorizontalContentAlignment="Center" 
                       FontSize="14" FontWeight="SemiBold" 
                       Content="{Binding Weight}" 
                       Style="{DynamicResource LeftLoginTextBoxBlockLabelStyle}"/>
                <Label Margin="0,2,0,0" Width="152" HorizontalAlignment="Center" HorizontalContentAlignment="Center" 
                       FontSize="14" 
                       Content="{Binding Size}" 
                       Style="{DynamicResource LeftLoginTextBoxBlockLabelStyle}"/>
            </StackPanel>
            <StackPanel Margin="0,34,0,0" x:Name="subactions" HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed">
                <ToggleButton x:Name="delete" IsTabStop="False" Width="70"
                              IsChecked="{Binding DataContext.CurrentViewModel.IsDeleteConfirmed, Mode=TwoWay, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                              Style="{StaticResource DeleteConfirmToggleButtonStyle}"
                              Command="{Binding DataContext.CurrentViewModel.ConfirmDeletePalletTypeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                              CommandParameter=""/>
                <Button x:Name="edit" IsTabStop="False" HorizontalAlignment="Left" Height="40" Style="{DynamicResource EditButtonStyle}" Content="Edit"
                        Command="{Binding Path=DataContext.CurrentViewModel.EditPalletTypeCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
                        CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}}"/>
            </StackPanel>
        </StackPanel>
        <DataTemplate.Triggers>
            <DataTrigger Binding="{Binding Path=IsChecked, ElementName=choose}" Value="True">
                <Setter Property="Visibility" TargetName="subactions" Value="Visible"/>
                <Setter Property="Height" TargetName="itemsactions" Value="200"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsMouseOver, ElementName=choose}" Value="True">
                <Setter Property="Source" TargetName="image" Value="{Binding ImageSource.MouseOver}"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsPressed, ElementName=choose}" Value="True">
                <Setter Property="Source" TargetName="image" Value="{Binding ImageSource.Pressed}"/>
                <Setter Property="Visibility" TargetName="subactions" Value="Visible"/>
                <Setter Property="Height" TargetName="itemsactions" Value="200"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsChecked, ElementName=choose}" Value="True">
                <Setter Property="Source" TargetName="image" Value="{Binding ImageSource.Choosen}"/>
            </DataTrigger>
            <DataTrigger Binding="{Binding IsEnabled, ElementName=choose}" Value="False">
                <Setter Property="Source" TargetName="image" Value="{Binding ImageSource.Disabled}"/>
                <Setter Property="Height" TargetName="itemsactions" Value="135"/>
            </DataTrigger>
        </DataTemplate.Triggers>
    </DataTemplate>

正常工作的ListBox演示:

正常工作的ListBox演示

内容的提问来源于stack exchange,提问作者Sid Brooklyn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 10:45:10