Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何将组合框大小设置为其内容的最大宽度?_.net_Wpf_Combobox - Fatal编程技术网

.net 如何将组合框大小设置为其内容的最大宽度?

.net 如何将组合框大小设置为其内容的最大宽度?,.net,wpf,combobox,.net,Wpf,Combobox,我有这个组合框 <ComboBox ItemsSource="{Binding Path=Foo.Bars}"/> 长度等于简·玛丽的长度 此外,在这种情况下,初始化后内容不会更改,您可以做的是创建一个转换器,该转换器将返回对象属性中最长的一个属性。您可以这样实现转换器: public class LongestListObjectToIntConverter : IValueConverter { public object Convert(object value, T

我有这个组合框

<ComboBox ItemsSource="{Binding Path=Foo.Bars}"/>
长度等于简·玛丽的长度


此外,在这种情况下,初始化后内容不会更改,您可以做的是创建一个转换器,该转换器将返回对象属性中最长的一个属性。您可以这样实现转换器:

public class LongestListObjectToIntConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is IEnumerable<FooBar>)
        {
            IEnumerable<FooBar> list = (IEnumerable<FooBar>)value;

            return list.Max(bar => bar.FullName.Length);
        }

        // Default value to return
        return 100;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
公共类LongestListObjectToIntConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
if(值为IEnumerable)
{
IEnumerable列表=(IEnumerable)值;
return list.Max(bar=>bar.FullName.Length);
}
//要返回的默认值
返回100;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
然后简单地绑定组合框的Width属性,方法是将列表作为路径绑定,将转换器作为值转换器

<Window.Resources>
    <conv:LongestListObjectToIntConverter x:Key=converter/>
</Windows.Resources>

    ...

<ComboBox ItemsSource="{Binding Path=Foo.Bars}" Width="{Binding Path=Foo.Bars, Converter={StaticResource converter}}"/>

...
这样,即使您的集合发生了更改并且没有通知这些更改,组合框也会根据最长的单词调整大小

另一个有趣的想法是对宽度进行自绑定,并在转换器中获取实际的组合框,然后检查显示的值,我认为这会更好


此解决方案的优点是不使用代码隐藏,并且易于重用。您可以在此处找到有关ValueConverter的更多信息:

下拉宽度可用于设置代码隐藏的宽度。请使用以下链接作为参考


但是,这将仅为下拉部分设置大小。

您可以这样编写转换器

 public class ComboBoxToMaxItemWidthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        double maxWidth = 0;
        ComboBox cb = (ComboBox)value;
        foreach (var item in cb.Items)
        {
            ComboBoxItem cbItem = (ComboBoxItem)cb.ItemContainerGenerator.ContainerFromItem(item);
            if (cbItem.ActualWidth > maxWidth)
                maxWidth = cbItem.ActualWidth;
        }
        return maxWidth;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
并使用它

<ComboBox ItemsSource="{Binding Path=Foo.Bars}" Width={Binding RelativeSource={RelativeSource Self}, Converter={StaticResource comboBoxToMaxItemWidthConverter }/>


希望这有帮助

@ArsenMkrt:cbItem由于SL中的已知问题而为空(此处链接:)。因此,目前这不是一个有效的解决方案

@YogWiN:通常情况下,combobox的项是模型对象(来自ItemsSource绑定),而不是回答中假设的ComboBoxItem控件。因此,无法迭代项集合

此外,我试图覆盖ComboBox上的PrepareContainerForItemOverride方法,但元素在下拉列表中显示之前并没有宽度。我还尝试自己切换默认面板,但MeasureOverride只有在下拉列表打开时才被调用


据我所知,今天,您无法使用ComboBox控件来执行此操作。

为什么您认为我的对象具有
全名
属性?如果对象是字符串,如何将字符串长度转换为像素?我任意使用了FullName。您可以使用任何想要的属性,甚至可以将它们组合在一起,如:bar=>(bar.FirstName+bar.LastName).lengh。再看看我给你的附加解决方案。哪个附加解决方案?如果我使用字符串,像“Jane Mary”这样的名称将返回9(9个字符),但我认为实际像素宽度大于这个值。我给出的附加解决方案实际上就是ArsenMkrt刚才给你的解决方案。跟着这个家伙;)当然,对于大小,您必须根据组合框的边框厚度和扩展器的宽度添加额外的像素。这是最好的方法。我想是吧。只需添加一些额外的像素,因为ComboBox的扩展器和边框厚度。我在这一行中得到编译错误
ComboBoxItem cbItem=cb.ItemContainerGenerator.ContainerFromItem(item)关于类型不匹配的内容。如何修复它?抱歉,我在这里手动编写了代码,所以没有注意到错误,使用它修复了nowI treid,但是cbItem为null(即使item不是,cb.Items有2个成员),知道为什么会发生这种情况吗?看起来它不起作用,因为转换器是在项目绑定到组合框之前调用的。这是针对Windows窗体的,而不是WPF。
<ComboBox ItemsSource="{Binding Path=Foo.Bars}" Width={Binding RelativeSource={RelativeSource Self}, Converter={StaticResource comboBoxToMaxItemWidthConverter }/>