WPF中的GridView动态列宽

WPF中的GridView动态列宽,wpf,gridview,listview,Wpf,Gridview,Listview,给出上面的示例新闻名称,然后是长名称。为什么列的大小不能适应其内容?试试看 public partial class Window1 : Window { class TextObject { private string _text; public TextObject(string Text) { _text = Text; } public string Text

给出上面的示例新闻名称,然后是长名称。为什么列的大小不能适应其内容?

试试看

public partial class Window1 : Window
{
    class TextObject
    {
        private string _text;

        public TextObject(string Text)
        {
            _text = Text;
        }

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
    }


    public Window1()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        CheckoutList.Items.Add(new TextObject("name"));
    }

    private void Button_Click1(object sender, RoutedEventArgs e)
    {
        CheckoutList.Items.Add(new TextObject("long name"));
    }

}

这将有助于调整列的大小以适应标题内容。

这是可行的,因为将列宽设置为NaN将允许该列测量到无穷大,从而填充其父容器的所有空间。但是是否存在一个
双.PositiveInfinity
?这种行为是被编程的(就像他们在
gridview列的代码中检查
Double.IsNan(width)
并将其设置为
Auto
),还是由于
Double.Nan
本身的性质?这不是Double.Nan的性质,而是WPF测量其可用宽度与容器宽度的性质。通过将列宽设置为Double.NaN将通过“不设置固定宽度”来“愚弄”WPF,这会产生“自动”宽度的错觉。这与
width=“Auto”
-至少对我来说是一样的,因为我得到了相同的结果。@imekon据我所知width=“Auto”存在性能问题,因为它应该等待加载所有行,以便定义其宽度。实际上不建议在数据列上使用它。
public partial class Window1 : Window
{
    class TextObject
    {
        private string _text;

        public TextObject(string Text)
        {
            _text = Text;
        }

        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }
    }


    public Window1()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        CheckoutList.Items.Add(new TextObject("name"));
    }

    private void Button_Click1(object sender, RoutedEventArgs e)
    {
        CheckoutList.Items.Add(new TextObject("long name"));
    }

}
<GridViewColumn Width="{x:Static System:Double.NaN}">
xmlns:System="clr-namespace:System;assembly=mscorlib"