WPF ComboBox弹出窗口布局:底部并与右边缘对齐

Tho*_*que 21 wpf combobox placement drop-down-menu

我正在尝试创建一个ComboBox非标准的下拉对齐.基本上,我希望下拉列表位于下方ComboBox,但与ComboBox左边缘的右边缘对齐.

通常的ComboBox样子,使用PlacementMode="Bottom":

组合框左对齐

我想要的是:

组合框向右对齐

我试图Popup.PlacementMode在我的模板中使用该属性ComboBox,但没有一个可能的值似乎做我想要的.有没有一种简单的方法,最好是纯XAML?

vor*_*olf 41

当我打开Expression Blend时,我在几秒钟内就提出了解决方案:

<Popup Placement="Left" VerticalOffset="{TemplateBinding ActualHeight}" 
       HorizontalOffset="{TemplateBinding ActualWidth}"
Run Code Online (Sandbox Code Playgroud)

有时这个应用程序比手工编写xaml更有用,但不常见. 在此输入图像描述

  • 非常优雅 - 但是如果您将ComboBox放在Windows的右侧并在多显示器设置中最大化Window**,在最大化窗口的右侧有一个显示器,Popup将移动到显示器在右边:( (2认同)
  • @ springy76你应该避免讽刺.你不是建设性的,所以人们会忽视你. (2认同)

ser*_*nko 5

我将对PopUp使用“自定义”放置模式,并声明一个回调以将弹出控件放置在正确的位置,如此处所示:WPF ComboBox DropDown Placement

看看这里的示例是否适合您:

public class TestComboBox : ComboBox
{
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();

        var popup = (Popup)Template.FindName("PART_Popup", this);
        popup.Placement = PlacementMode.Custom;
        popup.CustomPopupPlacementCallback += (Size popupSize, Size targetSize, Point offset) => 
            new[] {  new CustomPopupPlacement() { Point = new Point (targetSize.Width-popupSize.Width, targetSize.Height) } };
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助,问候