请考虑以下控件/模板。
基本上,当IsMouseOver为true时,有额外的触发器/资源会导致控件展开/折叠。当控件折叠时,我希望TextBlock“ MySelectionInfo”显示所选项目的文本;当它展开时,我希望像正常一样显示项目列表。是否有一种方法可以在纯XAML中获取所选项目的文本并在TextBlock中显示?
设置Text =“ {TemplateBinding SelectedItem}”运行,但不显示任何内容。
编辑(解决方案):
"
<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时,有额外的触发器/资源会导致控件展开/折叠。当控件折叠时,我希望TextBlock“ MySelectionInfo”显示所选项目的文本;当它展开时,我希望像正常一样显示项目列表。是否有一种方法可以在纯XAML中获取所选项目的文本并在TextBlock中显示?
设置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”是我正在显示的项目类型的已知属性。
"