我正在制作Combobox,让它在Combobox按钮的顶部显示项目列表,但经过这么多解决方案,它仍然不起作用.我怎么才能做到呢?我需要一些帮助
在这么多方法后,仍然不起作用,对不起,因为我只是在WPF的新手.
我正在制作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>
结果: