【问题标题】:How to bind DataGrid column width to column width of parent grid如何将 DataGrid 列宽绑定到父网格的列宽
【发布时间】:2017-12-31 01:31:27
【问题描述】:

我有一份相当复杂的财务报告,其中包含许多页眉和摘要页脚,以及中间的表格数据。这是应该的样子:

页面的最顶层容器是一个 Grid,当页面调整大小时,其列应自动调整。在此网格中包含所有标题和汇总字段。中间是一个 DataGrid,它应该保存详细的财务数据,当然我需要让它的列的列宽与网格的列宽相同。我的问题是我无法将 DataGrid 列的宽度绑定到封闭 DataGrid 的宽度。代码如下:

        <Grid x:Name="gridKonsolidiert" 
          Grid.Row="1"
          HorizontalAlignment="Stretch"
          Margin="0,0,9,0">

        <Grid.RowDefinitions>
            <RowDefinition Height="28" />
            <RowDefinition Height="28" />
            <RowDefinition Height="22" />
            <RowDefinition Height="*" />
            <RowDefinition Height="22" />
            ....
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition x:Name="gcolEADatum" Width="50*" />
            <ColumnDefinition x:Name="gcolEinBtto20"  Width="60*" />
            <ColumnDefinition x:Name="gcolEinUSt20"  Width="60*" />
            <ColumnDefinition x:Name="gcolEinNtto20"  Width="60*" />
            ....
        </Grid.ColumnDefinitions>

        <DataGrid x:Name="dgvKonsolidiert"
                  Grid.Row="3"
                  Grid.Column="0"
                  Grid.ColumnSpan="21"
                  Focusable="True"
                  Width="{Binding ElementName=gridKonsolidiert, Path=ActualWidth}"
                  ItemsSource="{Binding KonsolidiertCol, Mode=OneWay}"
                  IsSynchronizedWithCurrentItem="True"
                  AlternationCount="2"
                  AlternatingRowBackground="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"
                  SelectionMode="Single"
                  SelectionUnit="FullRow"
                  AutoGenerateColumns="False"
                  GridLinesVisibility="None"
                  VerticalAlignment="Stretch"
                  HorizontalAlignment="Stretch"
                  RenderTransformOrigin="0.5,0.5"
                  CanUserAddRows="False"
                  HeadersVisibility="Column"
                  VerticalScrollBarVisibility="Hidden"
                  HorizontalScrollBarVisibility="Disabled"
                  CanUserReorderColumns="False"
                  CanUserResizeColumns="False"
                  CanUserResizeRows="False"
                  RowDetailsVisibilityMode="Visible"
                  BorderThickness="1"
                  BorderBrush="{StaticResource DataGridBorderColor}"
                  IsReadOnly="True"
                  Margin="0,2,0,20"
                  Grid.RowSpan="2">

            <DataGrid.Columns>

                <DataGridTextColumn x:Name="colEADatum"
                                    Header="Datum"
                                    HeaderStyle="{StaticResource DataGridHeaderCentered_BorderRight}"
                                    CellStyle="{StaticResource DataGridCellCentered_BorderRight}"
                                    Width="{Binding ElementName=gcolEADatum, Path=ActualWidth, diag:PresentationTraceSources.TraceLevel=High}"
                                    Binding="{Binding Path=EADatum, StringFormat=dd.MM}"
                                    TextBlock.LineHeight="24">
                </DataGridTextColumn>
                <DataGridTextColumn x:Name="colEinBtto20"
                                    Header="Brutto"
                                    HeaderStyle="{StaticResource DataGridHeaderRightAligned}"
                                    Width="60*"
                                    CellStyle="{StaticResource DataGridCellRightAligned}"
                                    Binding="{Binding Path=EinBtto20, StringFormat={StaticResource fmtNumberOrEmpty}}">
                </DataGridTextColumn>
                <DataGridTextColumn x:Name="colEinUSt20"
                                    Header="USt"
                                    HeaderStyle="{StaticResource DataGridHeaderRightAligned}"
                                    Width="60*"
                                    CellStyle="{StaticResource DataGridCellRightAligned}"
                                    Binding="{Binding Path=EinUSt20, StringFormat={StaticResource fmtNumberOrEmpty}}">
                </DataGridTextColumn>
                ....
            </DataGrid.Columns>
        </DataGrid>

输出窗口中的诊断然后指出:

    System.Windows.Data Warning: 56 : Created BindingExpression (hash=58206383) for Binding (hash=58663159)
    System.Windows.Data Warning: 58 :   Path: 'ActualWidth'
    System.Windows.Data Warning: 60 : BindingExpression (hash=58206383): Default mode resolved to OneWay
    System.Windows.Data Warning: 61 : BindingExpression (hash=58206383): Default update trigger resolved to PropertyChanged
    System.Windows.Data Warning: 62 : BindingExpression (hash=58206383): Attach to System.Windows.Controls.DataGridTextColumn.Width (hash=48266778)
    System.Windows.Data Warning: 64 : BindingExpression (hash=58206383): Use Framework mentor <null>
    System.Windows.Data Warning: 67 : BindingExpression (hash=58206383): Resolving source 
    System.Windows.Data Warning: 69 : BindingExpression (hash=58206383): Framework mentor not found
    System.Windows.Data Warning: 65 : BindingExpression (hash=58206383): Resolve source deferred
    'EARechnung_C.exe' (CLR v4.0.30319: EARechnung_C.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.resources\v4.0_4.0.0.0_de_31bf3856ad364e35\PresentationFramework.resources.dll'. Module was built without symbols.
    System.Windows.Data Warning: 67 : BindingExpression (hash=58206383): Resolving source 
    System.Windows.Data Warning: 69 : BindingExpression (hash=58206383): Framework mentor not found
    'EARechnung_C.exe' (CLR v4.0.30319: EARechnung_C.exe): Loaded 'C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Resources.ResourceManager\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Resources.ResourceManager.dll'. Cannot find or open the PDB file.
    System.Windows.Data Warning: 79 : BindingExpression (hash=58206383): Deactivate
    System.Windows.Data Warning: 63 : BindingExpression (hash=58206383): Detach

我希望有人可以帮助我解决这个问题。

【问题讨论】:

  • DataGridColumns 不是 Visual Tree 的一部分,因此绑定不起作用。你需要做一个工作。网上有很多例子。
  • Nikhil,如果您能将其中一个示例的链接发送给我,将会很有帮助。干杯
  • 谢谢尼基尔。该示例是关于如何将 DataGrid 列的可见性绑定到某物。我需要将 DataGrid 列的宽度绑定到 Grid 列的宽度,其中 Grid 是 DataGrid 的父级。我看不出如何将该示例应用于我的问题。你能再给我同样的解释吗?

标签: c# wpf datagrid


【解决方案1】:

基于 Nikhil 的 示例和他的提示“DataGridColumns are not part of Visual Tree”。

我做了以下事情:我添加了最后一个标题行并将DataGridColumns 的列标题定义为带有TextBlock 和名称的边框,例如x:Name="gcolEADatum"。 DataGrid 具有属性HeadersVisibility="None",每个DataGridColumn 现在通过Width="{Binding Source={x:Reference gcolEADatum}, Path=ActualWidth} 引用其标题的实际宽度。

这现在可以按预期工作,即如果页面调整了网格列的大小,并且数据网格列都正确调整了大小。

【讨论】:

  • 没问题的朋友! ;)
猜你喜欢
  • 1970-01-01
  • 2011-06-24
  • 2010-09-22
  • 2012-06-02
  • 2012-03-07
  • 1970-01-01
  • 2011-11-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多