如何绑定到 ControlTemplate 中的 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”是我正在显示的项目类型的已知属性。
Consider the following Control/Template
<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>
Basically, there are additional triggers/resources that cause the control to expand/collapse when IsMouseOver is true. When the control is collapsed, I'd like the TextBlock "MySelectionInfo" to display the selected item's text; when it's expanded, I'd like the list of items to be displayed like normal. Is there a way to grab the selected item's text and display it in the TextBlock in pure XAML?
Setting Text="{TemplateBinding SelectedItem}" runs, but doesn't display anything.
EDIT (Solution):
<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" is a known property of the type of item I'm displaying.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
RelativeSource
进行绑定是否可行? 也许是这样的:Would it work to bind using a
RelativeSource
instead? Maybe something like this: