如何使 WPF 组合框具有 XAML 中其最宽元素的宽度? [英] How can I make a WPF combo box have the width of its widest element in XAML?

查看:35
本文介绍了如何使 WPF 组合框具有 XAML 中其最宽元素的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何在代码中完成,但这可以在 XAML 中完成吗?

I know how to do it in code, but can this be done in XAML ?

Window1.xaml:

Window1.xaml:

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid>
        <ComboBox Name="ComboBox1" HorizontalAlignment="Left" VerticalAlignment="Top">
            <ComboBoxItem>ComboBoxItem1</ComboBoxItem>
            <ComboBoxItem>ComboBoxItem2</ComboBoxItem>
        </ComboBox>
    </Grid>
</Window>

Window1.xaml.cs:

Window1.xaml.cs:

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

namespace WpfApplication1
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            double width = 0;
            foreach (ComboBoxItem item in ComboBox1.Items)
            {
                item.Measure(new Size(
                    double.PositiveInfinity, double.PositiveInfinity));
                if (item.DesiredSize.Width > width)
                    width = item.DesiredSize.Width;
            }
            ComboBox1.Measure(new Size(
                double.PositiveInfinity, double.PositiveInfinity));
            ComboBox1.Width = ComboBox1.DesiredSize.Width + width;
        }
    }
}

推荐答案

如果没有这两者,就不能在 XAML 中:

This can't be in XAML without either:

  • 创建隐藏控件(Alan Hunford 的回答)
  • 彻底改变 ControlTemplate.即使在这种情况下,也可能需要创建 ItemsPresenter 的隐藏版本.

这样做的原因是我遇到的默认 ComboBox ControlTemplates(Aero、Luna 等)都将 ItemsPresenter 嵌套在弹出窗口中.这意味着这些项目的布局会推迟到它们实际可见时才进行.

The reason for this is that the default ComboBox ControlTemplates that I've come across (Aero, Luna, etc.) all nest the ItemsPresenter in a Popup. This means that the layout of these items is deferred until they are actually made visible.

一个简单的测试方法是修改默认的 ControlTemplate 以将最外层容器(它是 Aero 和 Luna 的网格)的 MinWidth 绑定到 PART_Popup 的 ActualWidth.当您单击下拉按钮时,您将能够让 ComboBox 自动同步其宽度,但之前不会.

An easy way to test this is to modify the default ControlTemplate to bind the MinWidth of the outermost container (it's a Grid for both Aero and Luna) to the ActualWidth of PART_Popup. You'll be able to have the ComboBox automatically synchronize it's width when you click the drop button, but not before.

因此,除非您可以在布局系统中强制执行 Measure 操作(您可以通过添加第二个控件来实现),否则我认为它无法完成.

So unless you can force a Measure operation in the layout system (which you can do by adding a second control), I don't think it can be done.

和往常一样,我愿意接受一个简短、优雅的解决方案——但在这种情况下,代码隐藏或双控制/ControlTemplate hacks 是我见过的唯一解决方案.

As always, I'm open to an short, elegant solution -- but in this case a code-behind or dual-control/ControlTemplate hacks are the only solutions I have seen.

这篇关于如何使 WPF 组合框具有 XAML 中其最宽元素的宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆