2

I have a fairly complex financial report with a number of headers and summarizing footers and with tabular data inbetween. Here is what is supposed to look like: enter image description here

The topmost container of the page is a Grid whose columns should automatically adjust when the page is resized. In this Grid all headers and summary fields are contained. Inbetween is a DataGrid that is supposed to hold the detailed financial data and of course I need to have the column width of it's columns the same size as those of the Grid. My problem is that I am not able to bind the width of the DataGrid columns to the width of the enclosing DataGrid. Here is the code:

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

The diagnose in Output Window then states:

    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

I hope there is anybody that can help me with that problem.

pb_SKAT
  • 163
  • 2
  • 14
  • DataGridColumns are not part of Visual Tree so binding won't work. You need to do a work around. Plenty of examples over the internet. – Nikhil Agrawal Jul 25 '17 at 08:52
  • It would be helpfull if you could send me a link to one of those examples, Nikhil. Cheers – pb_SKAT Jul 25 '17 at 09:04
  • https://stackoverflow.com/questions/22073740/binding-visibility-for-datagridcolumn-in-wpf – Nikhil Agrawal Jul 25 '17 at 09:14
  • Thanks Nikhil. The example is about how to bind the visibility of a DataGrid column to something. What I need is to bind is the width of a DataGrid column to the width of a Grid column where the Grid is parent of the DataGrid. I can't see how to apply that example to my problem. Could you please give me same more explanation? – pb_SKAT Jul 25 '17 at 11:27

1 Answers1

2

Based on Nikhil's example and his hint "DataGridColumns are not part of Visual Tree".

I did the following: I added a last header row and defined the column headers of the DataGridColumns as borders with TextBlock and a name, e.g. x:Name="gcolEADatum". The DataGrid has the property HeadersVisibility="None" and each DataGridColumn now references the actual width of its header through Width="{Binding Source={x:Reference gcolEADatum}, Path=ActualWidth}.

This now works as expected, i.e. if the page is resized the grid columns and the datagrid columns both resize properly.

Travis Tubbs
  • 827
  • 1
  • 14
  • 32
pb_SKAT
  • 163
  • 2
  • 14