【问题标题】:GridViewColumn Width AdjustmentGridViewColumn 宽度调整
【发布时间】:2012-02-23 10:21:06
【问题描述】:

我的用户界面:

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

我的 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()。列不会调整大小。

a) 我怎样才能自动(当我调用更新时)将“名称”列调整为最长名称的长度,但不超过值 x(我想指定列的最大宽度)?

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

【问题讨论】:

    标签: c# .net wpf xaml gridview


    【解决方案1】:

    GridView 不会自动调整大小。

    你可以调整列的大小

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

    至于调整最后一个填充区域的大小,我使用转换器来完成。我认为这个转换器不能完全满足你的需要,但它应该让你开始。

        <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 速度很快,但需要一些婴儿坐姿。

    【讨论】:

    • 我知道这已经很老了,但我想强调一下,ColumnWidth 修复的时机是多么重要,并且在正确应用时它确实运作良好。确保 GridView 容器已加载且可见。我有一个非常讨厌的组合 GridView 被放置在扩展器中,而扩展器又是虚拟化父控件的一部分。这意味着,在屏幕外调整大小将产生零宽度,并且当模板被回收用于不同数据时,项目可能会发生变化。现在我对我的解决方案有点满意。
    【解决方案2】:

    我从 How to autosize and right-align GridViewColumn data in WPF? 那里提取了这个

    <Window.Resources>
        <Style TargetType="ListViewItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
        </Style>
    </Window.Resources>
    

    这对你有帮助吗?

    【讨论】:

      猜你喜欢
      • 2017-08-20
      • 2011-10-08
      • 2019-10-09
      • 2011-10-15
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多