【问题标题】:WPF datagrid MinWidth=auto MaxWidth=*WPF 数据网格 MinWidth=auto MaxWidth=*
【发布时间】:2020-03-30 18:32:09
【问题描述】:

我有一个看似简单的用例,但它困扰了我好几个小时

我有一个两行两列的网格,第一列应采用所有可用宽度,第二列应采用所需宽度。问题在于第二列的“所需宽度”。此列在顶行包含一个数据网格,在底行包含一个标签(为简单起见)。 数据网格有两列,它们都应占数据网格的 50% 宽度。

我想要:

  • 如果标签比数据网格宽,则数据网格应按比例放大(数据网格末尾可能不会出现半列,填满剩余空间)
  • 如果数据网格比标签宽,列可能不是 小于数据网格所需的大小。

问题:

  • 如果我将数据网格列设置为占用所有可用空间(宽度='*'),如果标签大于数据网格(但如果标签更小,数据网格会缩小到标签的宽度,而不是所有文字都可读)
  • 如果我将列设置为“自动调整大小”,它的工作原理是标签小于数据网格(但如果标签更大,则会出现“半”列(这真的很伤我的眼睛)。

代码:

x:Class="WpfApp1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="200" Width="400">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Grid Grid.Column="1" Name="grid">
        <!-- Change column width to simulate the problemn-->
        <DataGrid ItemsSource="{Binding Items}" ColumnWidth="*" RowHeaderWidth="0" Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Grid}}}"/>
    </Grid>
    <!-- change label text to simulate problemn-->
    <Label Grid.Row="1" Grid.Column="1" Content="x"/>
</Grid>

  • 绑定到网格的实际宽度是让列正确放大
  • Items 是一个对象列表(包含两个字符串值)

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    我最终创建了自己的用户控件。

    这相对容易,因为我在设计时就知道“datagridview”中有哪些行。

    Tronald 的建议有效,前提是数据在渲染后不发生变化(我的建议是,数据网格内容和“标签”都会根据应用程序其余部分中的选择而变化)。

    我仍然认为数据网格应该可以使用 'Width="*" MinWidth="auto"' 组合,所以如果有人有解决方案。

    【讨论】:

    • 自定义控件是适合您的情况的方法。我很高兴你选择了那条路而不是我的路。
    【解决方案2】:

    这很脏,可能会让我重新考虑我的设计,但它可以根据您的要求工作。我不确定如何在不创建自定义控件或大幅修改DataGrid 默认模板的情况下实现此目的,但这里可以。

    将您的 DataGrid 样式更改为此,并为 LabelDataGrid 命名。

    <DataGrid Name="dg" ItemsSource="{Binding Items}" CanUserResizeColumns="False"/>
    ///
    <Label Grid.Row="1" Grid.Column="1" Content="x" Name="label"/>
    

    然后为您的MainWindow 创建一个ContentRendered 事件。

     Title="MainWindow" Height="450" Width="800" Name="mw" ContentRendered="Mw_ContentRendered">
    

    现在在渲染事件中,在渲染发生后手动调整列的大小。您无法为宽度指定星号,因为它不会正确调整大小,因此您需要根据列数平均划分宽度。

    private void Mw_ContentRendered(object sender, EventArgs e)
    {    
         //Loop through columns and resize
         for (int x = 0; x < dg.Columns.Count; x++)
         {
             double div = dg.ActualWidth / dg.Columns.Count;
             double add = div - dg.Columns[x].ActualWidth;
             if (add < 0) { div += -add; }
             dg.Columns[x].Width = new DataGridLength(div);         
         }     
    }
    

    编辑:更新宽度逻辑以解决不均匀宽度

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-16
      • 2018-03-09
      • 2015-10-10
      • 2018-01-11
      • 2018-06-05
      • 2017-06-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多