我正在制作Combobox,让它在Combobox按钮的顶部显示项目列表,但经过这么多解决方案,它仍然不起作用.我怎么才能做到呢?我需要一些帮助

在这么多方法后,仍然不起作用,对不起,因为我只是在WPF的新手.

推荐答案

您可以派生组合框并使用CustomPopupPlacementCallback放置弹出窗口:

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;

namespace WpfApp4
{
    class MyComboBox : ComboBox
    {
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            var popup = (Popup)Template.FindName("PART_Popup", this);
            popup.Placement = PlacementMode.Custom;
            popup.CustomPopupPlacementCallback = placePopup;
        }

        private CustomPopupPlacement[] placePopup(Size popupSize, Size targetSize, Point offset)
        {
            var placements = new[] { new CustomPopupPlacement() };
           // adapt the value of point to place..
            placements[0].Point = new Point(0, -100);
            return placements;
        }

并在XAML中使用它:

<Window x:Class="WpfApp4.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp4"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <local:MyComboBox x:Name="cb" Width="100" Height="100" >
            <ComboBoxItem Content="Alice"/>
            <ComboBoxItem Content="Bob"/>
            <ComboBoxItem Content="Charlie"/>
        </local:MyComboBox>
    </Grid>
</Window>

结果:

enter image description here

Csharp相关问答推荐

在包含空项的列表上使用具有断言T的摘要表

为什么我不能更改尚未设置的模拟对象属性的值?

. NET 8 HttpClient post参数将其情况更改为camel'

我无法在Ubuntu下编译使用microsoft.extension.configurationbuilder jsonapi和mono mcs的c#应用程序

有没有一种方法可以防止在编译时在MicrosoftC或非单线程上下文中调用方法?

HttpContext. RequestAborted当Android APP失go 连接时未取消

CS0103 dlibdotnet和www.example.com facerect不在上下文中

. NET在上一个操作完成之前,在此上下文实例上启动了第二个操作

安装附加的. exe与Visual Studio

HttpConext.Request.Path和HttpConext.GetEndpoint()之间的差异

UWP中的任务和界面

如何将此方法参数化并使其更灵活?

在C#中,将两个哈希集连接在一起的时间复杂度是多少?

自定义列表按字符串的部分排序

.NET 6:如何防止系统生成的日志(log)?

在implementationFactory中避免循环依赖

在PostgreSQL上使用ExecuteSqlRawAsync的C#11原始字符串文字有区分大小写的问题

工厂类是如何在.NET 8中注册的?

如何获取我在SQL中输入的值

Roslyn编译器看不到引用