我怎样才能让一个WPF组合框在XAML最宽处元素的宽度? [英] How can I make a WPF combo box have the width of its widest element in XAML?

查看:108
本文介绍了我怎样才能让一个WPF组合框在XAML最宽处元素的宽度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何做到这一点的代码,但是这可以在XAML来完成



Window1.xaml:

 <窗​​口x:类=WpfApplication1.Window1
的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/presentation
的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
标题=窗口1HEIGHT =300WIDTH =300>
<网格和GT;
<组合框名称=ComboBox1的Horizo​​ntalAlignment =左VerticalAlignment =评出的>
< ComboBoxItem> ComboBoxItem1< / ComboBoxItem>
< ComboBoxItem> ComboBoxItem2< / ComboBoxItem>
< /组合框>
< /网格和GT;
< /窗GT;



Window1.xaml.cs:

 使用System.Windows;使用System.Windows.Controls的
;

命名空间WpfApplication1
{
公共部分类窗口1:窗口
{
公共窗口1()
{
的InitializeComponent() ;
双倍宽度= 0;
的foreach(在ComboBox1.Items ComboBoxItem项)
{
item.Measure(新尺寸(
double.PositiveInfinity,double.PositiveInfinity));
如果(item.DesiredSize.Width>宽度)
宽度= item.DesiredSize.Width;
}
ComboBox1.Measure(新尺寸(
double.PositiveInfinity,double.PositiveInfinity));
ComboBox1.Width = ComboBox1.DesiredSize.Width +宽度;
}
}
}


解决方案

这不能在XAML没有之一:




  • 创建一个隐藏的控制(阿兰Hunford的答案)

  • 更改ControlTemplate中急剧下降。即使在这种情况下,一个ItemsPresenter的隐藏版本可能需要创建



这样做的原因是,默认组合框CONTROLTEMPLATES我已经遇到(航空,卢纳等)都窝ItemsPresenter在弹出。这意味着,这些项目的布局推迟,直到他们实际上是可见。



一个简单的方法来测试,这是修改默认的ControlTemplate绑定的了了minWidth最外层容器(它是为航空和月神网格),以PART_Popup的ActualWidth的。你就可以自动拥有组合框同步它的宽度当您单击下拉按钮,而不是之前。



所以,除非你可以强制布局中测量操作系统(您可以通过添加第二个控制做),我不认为它可以做到的。



像往常一样,我M:打开一个简短,优雅的解决方案 - 但在这种情况下,代码隐藏或双控/控件模板黑客是我见过的唯一的解决办法


I know how to do it in code, but can this be done in 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:

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;
        }
    }
}

解决方案

This can't be in XAML without either:

  • Creating a hidden control (Alan Hunford's answer)
  • Changing the ControlTemplate drastically. Even in this case, a hidden version of an ItemsPresenter may need to be created.

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.

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.

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.

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天全站免登陆