我想将我在 HeaderTemplate 中使用的 DockPanel 的宽度设置为列的实际宽度,并且正在努力进行绑定。
<DataTemplate x:Key="MyHeaderTemplate">
<DockPanel HorizontalAlignment="Stretch"
LastChildFill="False"
Width="{Binding Width, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridColumnHeader}}}">
<!-- Several items here. Some docked to the left side, some to the right-->
</DockPanel>
</DataTemplate>
这给了我一个绑定错误,并在标题的左侧将所有项目压缩在一起。
[编辑]
绑定到ActualWidth
而不是Width
给了我这两个绑定错误并增加了列的宽度太多,因此它右侧的所有列似乎都被“推出”了可见区域:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=AreRowDetailsFrozen; DataItem=null; target element is 'DataGridDetailsPresenter' (Name=''); target property is 'SelectiveScrollingOrientation' (type 'SelectiveScrollingOrientation')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.DataGrid', AncestorLevel='1''. BindingExpression:Path=HeadersVisibility; DataItem=null; target element is 'DataGridRowHeader' (Name=''); target property is 'Visibility' (type 'Visibility')
[编辑]
我不在乎,布局控件是 DockPanel 还是其他东西,只要它填满标题,我可以在左边安排一个 TextBlock,在右边安排几个图标......
[编辑]
我想要的是“左”显示在左边,“右”显示在右边。现在是左边的“LeftRight”。
<UserControl x:Class="Lb.Abrechnung.Kunden.Fbs.Client.Rechnungslauf.Dialoge.RechnungslaufWizard.Rechnungsvorschlaege.RechnungsvorschlagListe.TestView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
HorizontalContentAlignment="Stretch">
<Grid>
<DataGrid Background="Black">
<DataGrid.ColumnHeaderStyle>
<Style TargetType="DataGridColumnHeader">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Foreground" Value="White" />
</Style>
</DataGrid.ColumnHeaderStyle>
<DataGrid.Columns>
<DataGridTextColumn>
<DataGridTextColumn.HeaderTemplate>
<DataTemplate>
<DockPanel Width="{Binding Width, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGridColumnHeader}}}">
<TextBlock Text="Left" DockPanel.Dock="Left"/>
<TextBlock Text="Right" DockPanel.Dock="Right"/>
</DockPanel>
</DataTemplate>
</DataGridTextColumn.HeaderTemplate>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>