我有一个相当复杂的财务报告,其中包含许多标题和摘要页脚以及介于两者之间的表格数据。这是应该看起来像:
页面的最顶层容器是一个Grid,其页面调整大小时应自动调整列。在此网格中包含所有标题和摘要字段。 Inbetween是一个DataGrid,它应该包含详细的财务数据,当然我需要让它的列宽与Grid的列宽相同。我的问题是我无法将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
我希望有人可以帮我解决这个问题。
答案 0 :(得分:2)
基于 Nikhil的示例,他的提示“DataGridColumns不是Visual Tree的一部分”。
我执行了以下操作:我添加了最后一个标题行,并将DataGridColumns
的列标题定义为与TextBlock
的边界和名称,例如x:Name="gcolEADatum"
。 DataGrid具有属性HeadersVisibility="None"
,并且每个DataGridColumn现在通过Width="{Binding Source={x:Reference gcolEADatum}, Path=ActualWidth}
引用其标头的实际宽度。
现在按预期工作,即如果页面调整大小,网格列和datagrid列都会正确调整大小。