GridView 列宽调整 [英] GridViewColumn Width Adjustment

查看:51
本文介绍了GridView 列宽调整的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户界面:

<ListView Name="persons" SelectionChanged="persons_SelectionChanged">
        <ListView.View>
            <GridView AllowsColumnReorder="False">
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" Width="auto"/>
                <GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" Width="auto"/>
            </GridView>
        </ListView.View>
    </ListView>

我的用户界面的代码隐藏:

The Codebehind of my UI:

internal void Update(IEnumerable<Person> pers)
    {
        this.persons.ItemsSource = null;
        this.persons.ItemsSource = pers;
        UpdateLayout();
    }

我的实体:

class Person
{
   public string Name { get; set; }
   public int Age { get; set; }
}

GridViewColumns 具有 GridViewColumn-Header 的宽度.即使我用长名字的人调用 Update() .列未调整大小.

The GridViewColumns have the width of the GridViewColumn-Header. Even if i call Update() with Persons with a long name. The Column does not get resized.

a) 如何自动(当我调用 Update 时)将名称"列的大小调整为最长名称的长度,但不超过值 x(我想指定列的最大宽度)?

a) How can i resize the "Name"-column automatically (when i called Update) to the length of the longest Name, but not longer than a Value x (i want to specify a maximum width of a column)?

b) 我如何指定Age"-Column 填充控件末端的空间(以便 gridview 的列使用控件的完整宽度)?

b) How can i specify that the "Age"-Column fills the space to the control´s end (so that the columns of the gridview uses the complete width of the control)?

推荐答案

GridView 不会自动调整大小.

GridView does not resize automatically.

要调整列的大小,您可以

To resize the columns you can

    foreach (GridViewColumn c in gv.Columns)
    {
        // Code below was found in GridViewColumnHeader.OnGripperDoubleClicked() event handler (using Reflector)
        // i.e. it is the same code that is executed when the gripper is double clicked
        // if (adjustAllColumns || App.StaticGabeLib.FieldDefsGrid[colNum].DispGrid)
        if (double.IsNaN(c.Width))
        {
            c.Width = c.ActualWidth;
        }
        c.Width = double.NaN;
     }  

至于调整最后一个填充区域的大小,我用转换器来完成.我认为这个转换器不能完全满足您的需求,但它应该可以帮助您入门.

As for sizing the last to fill area I do it with a converter. I don't think this converter does exactly what you need for this but it should get you started.

    <GridViewColumn Width="{Binding ElementName=lvCurDocFields, Path=ActualWidth, Converter={StaticResource widthConverter}, ConverterParameter=100}">


    [ValueConversion(typeof(double), typeof(double))]
    public class WidthConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            // value is the total width available
            double otherWidth;
            try
            {
                otherWidth = System.Convert.ToDouble(parameter);
            }
            catch
            {
                otherWidth = 100;
            }
            if (otherWidth < 0) otherWidth = 0;

            double width = (double)value - otherWidth;
            if (width < 0) width = 0;
            return width; // columnsCount;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

GridView 速度很快,但需要一点时间.

GridView is fast but it takes a bit of baby sitting.

这篇关于GridView 列宽调整的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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