以下是代码:
<GridViewColumn DisplayMemberBinding="{Binding Path=BookId}" Width="100">
<GridViewColumn.Header>
<Border BorderBrush="Black">
<TextBlock Width="{Binding RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type GridViewColumn}},
Path=Width}" Text="ID">
<TextBlock.ContextMenu>
<ContextMenu>item1</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</Border>
</GridViewColumn.Header>
</GridViewColumn>
基本上我要做的是让标题中的TextBlock跟随整列的宽度。
它不起作用:文本块的宽度始终与内部文本匹配。有任何想法吗?... 提前谢谢!
答案 0 :(得分:14)
您的代码中存在两个问题
所以你的代码变成了:
<GridViewColumn
DisplayMemberBinding="{Binding Path=BookId}"
Width="100">
<GridViewColumn.Header>
<Border BorderBrush="Black" >
<TextBlock Text="ID" Width="{Binding RelativeSource=
{RelativeSource FindAncestor,
AncestorType={x:Type GridViewColumnHeader}},
Path=ActualWidth}">
<TextBlock.ContextMenu>
<ContextMenu>item1</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</Border>
</GridViewColumn.Header>
</GridViewColumn>