Wpf 如何绑定到ControlTemplate中的SelectedItem属性?

Wpf 如何绑定到ControlTemplate中的SelectedItem属性?,wpf,data-binding,xaml,controltemplates,Wpf,Data Binding,Xaml,Controltemplates,考虑以下控件/模板 <my:ExpandingListBox Margin="0,2,0,0" x:Name="TagSearchListBox"> <my:ExpandingListBox.Template> <ControlTemplate TargetType="{x:Type my:ExpandingListBox}"> <Border Name="MyBorder" BorderThickness

考虑以下控件/模板

<my:ExpandingListBox Margin="0,2,0,0" x:Name="TagSearchListBox">
    <my:ExpandingListBox.Template>
        <ControlTemplate TargetType="{x:Type my:ExpandingListBox}">
            <Border Name="MyBorder" BorderThickness="2" BorderBrush="Black">
                <Grid>
                    <TextBlock Name="MySelectionInfo" Background="White" Text="{TemplateBinding SelectedItem}"/>
                    <ScrollViewer Name="MyScrollViewer" HorizontalScrollBarVisibility="Hidden" Opacity="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" VerticalScrollBarVisibility="Hidden">
                        <ItemsPresenter Name="MyItemsPresenter"/>
                    </ScrollViewer>
                </Grid>
            </Border>
        </ControlTemplate>
    </my:ExpandingListBox.Template>
</my:ExpandingListBox>

基本上,当IsMouseOver为true时,会有额外的触发器/资源导致控件展开/折叠。当控件折叠时,我希望文本块“MySelectionInfo”显示所选项目的文本;当它展开时,我希望项目列表像正常一样显示。有没有办法获取所选项目的文本并在纯XAML的文本块中显示它

Setting Text=“{TemplateBinding SelectedItem}”运行,但不显示任何内容

编辑(解决方案):

<TextBlock Name="MySelectionInfo" Background="White">
    <TextBlock.Text>
        <Binding Path="SelectedItem.Name">
            <Binding.RelativeSource>
                <RelativeSource Mode="FindAncestor" AncestorType="{x:Type my:ExpandingListBox}"/>
            </Binding.RelativeSource>
        </Binding>
    </TextBlock.Text>
</TextBlock>


“.Name”是我正在显示的项类型的已知属性。

使用
相对资源进行绑定是否可行?也许是这样的:

<TextBlock Name="MySelectionInfo" Background="White"
    Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type my:ExpandingListBox}}}" />

效果很好!解决方案见上文:)