【发布时间】:2017-05-18 17:00:20
【问题描述】:
我们有一个带有自定义ListBoxItemTemplate 的ListBox,它使用ListBox 的DisplayMemberPath 属性显示一些信息。
ListBoxItemTemplate 内部有一个ContentPresenter。
我们需要做的就是设置ContentPresenter 的Tooltip 属性以显示与ContentPresenter 相同的内容。
我试过这样做:
<ContenPresenter Tooltip={Path Content, RelativeSource={RelativeSource Self}}/>
但我得到的上下文没有 DisplayMemberPath 逻辑(整个 datacontext 对象)。
如何获取应用了“DisplayMemberPath”的 ContentPresenter 显示的值?
编辑
这里没有Tooltip的样式(控件设置了这个样式和DisplayMemberPath属性绑定):
<Style x:Key="CheckListBoxStyle" TargetType="{x:Type ListBox}" >
<Setter Property="SelectionMode" Value="Multiple" />
<Setter Property="ItemContainerStyle" Value="{StaticResource CheckListBoxItemStyle}"/>
<Setter Property="Width" Value="177"/>
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Height" Value="70"/>
</Style>
<Style x:Key="CheckListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent" />
<Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" />
<Setter Property="Padding" Value="2,0,0,0" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding="{TemplateBinding Padding}"
SnapsToDevicePixels="true">
<CheckBox HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Tag="CheckBox1">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</CheckBox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
【问题讨论】:
-
我认为不可能同时为列表框同时使用 DisplayMemberPath 属性和 ItemTemplate。能否请您出示您的代码?
标签: c# wpf data-binding binding