English 中文(简体)
亲属 FindAncestor 问题
原标题:RelativeSource FindAncestor issue
  • 时间:2009-08-24 14:26:34
  •  标签:

该守则是:

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

基本上,我试图做的是使标题中的文字B锁在整栏的宽度之后。

It is not working: the textblock s width always matches the text inside. Any ideas?... Thanks in advance!

最佳回答

你的法典有两个问题。

  1. the GridViewColumn is NOT a visual ancestor of the TextBox, its ancestor is a GridViewColumnHeader
  2. You should bind to the ActualWidth of the GridViewColumnHeader, not the Width (if Width is not specified, it will be an invalid number)

因此,你的法典成为:

              <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>
问题回答

暂无回答




相关问题
热门标签