4

如何绑定到样式中的祖先?

我想做以下事情(试图让列宽自动与列表视图对齐)

<Style x:Key="m_ListViewStyle" TargetType="{x:Type ListView}" x:Shared="False">
<Setter Property="View">
    <Setter.Value>
        <GridView>
            <GridViewColumn>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        ...
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>

            <GridViewColumn Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"/>
        </GridView>
    </Setter.Value>
</Setter>

但我得到绑定错误 -

Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ListView', AncestorLevel='1''.
4

0 回答 0