我们有一个ListBox
与自定义ListBoxItemTemplate
,使用DisplayMemberPath
的ListBox
属性显示一些信息。
ListBoxItemTemplate
里面有一个ContentPresenter
。
我们需要做的就是设置ContentPresenter
的Tooltip属性来显示与ContentPresenter
相同的东西。
我试过这样做:
<ContenPresenter Tooltip={Path Content, RelativeSource={RelativeSource Self}}/>
但我得到没有DisplayMemberPath
逻辑(整个datacontext对象)的上下文。
如何在应用“DisplayMemberPath”的情况下获取ContentPresenter显示的值?
这里没有Tooltip的样式(控件设置了这个样式,DisplayMemberPath
属性带有Binding):
<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”的情况下获取ContentPresenter显示的值?
您无法在纯XAML中动态执行此操作。如果您知道DisplayMemberPath属性的值,则可以直接绑定到此属性:
<CheckBox HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Tag="CheckBox1"
ToolTip="{Binding Path=Content.Name, ElementName=cp}">
<ContentPresenter x:Name="cp" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</CheckBox>
但你不能做像ToolTip="{Binding Path=Content.[DisplayMemberPath], ElementName=cp}"
这样的事情。
您必须编写一些代码才能实现此目的。例如,您可以使用转换器:
public class ContentConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string path = values[0] as string;
object dataObject = values[1];
if(!string.IsNullOrEmpty(path) && dataObject != null)
return dataObject.GetType().GetProperty(path).GetValue(dataObject).ToString();
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
<ListBox x:Name="lb11" DisplayMemberPath="Name">
<ListBox.Resources>
<local:ContentConverter x:Key="conv" />
</ListBox.Resources>
<ListBox.Style>
<Style TargetType="{x:Type ListBox}" >
<Setter Property="SelectionMode" Value="Multiple" />
<Setter Property="ItemContainerStyle">
<Setter.Value>
<Style 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">
<CheckBox.ToolTip>
<MultiBinding Converter="{StaticResource conv}">
<Binding Path="DisplayMemberPath" RelativeSource="{RelativeSource AncestorType=ListBox}"/>
<Binding Path="Content" ElementName="cp"/>
</MultiBinding>
</CheckBox.ToolTip>
<ContentPresenter x:Name="cp" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</CheckBox>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Setter.Value>
</Setter>
<Setter Property="Width" Value="177"/>
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="Height" Value="70"/>
</Style>
</ListBox.Style>
</ListBox>
由于您已在模板中定义了ContentPresenter
,因此无法在Binding中访问内容。根据DataContext
分布,你已经在内容级别。所以它没有工作,我想改变ContentPresenter
到ContentControl
和ToolTip
像下面将工作
<ContentControl Tooltip={Binding}/>
如我错了请纠正我。