下面列表框的工具提示是使用 setter 设置的。鼠标悬停时工具提示不会出现。
我怀疑问题出在列表框本身的项目源上。列表框绑定(bind)到名为 CandidateAttributes 的 AttributeItems 列表。该列表的一个元素是一个名为 AttributePath 的可观察集合,我试图将工具提示绑定(bind)到的属性路径中的属性称为 ConceptualPath。下面是 CandidateAttributes 的定义-
public static List<AttributeItem> CoBRRaAttributes { get; set; }
AttributeItems 类-
public class AttributeItem
{
private string _displayName = "";
private ObservableCollection<CoBRRa_WPF.CoBRRaUtilities.ViewModels.QueryTool.AttributeCollection> _AttributePath;
public AttributeItem(int id, string displayName, ObservableCollection<CoBRRa_WPF.CoBRRaUtilities.ViewModels.QueryTool.AttributeCollection> attributePath)
{
DisplayName = displayName;
AttributePath = attributePath;
}
public ObservableCollection<CoBRRa_WPF.CoBRRaUtilities.ViewModels.QueryTool.AttributeCollection> AttributePath
{
get
{
return _AttributePath;
}
set
{
_AttributePath = value;
}
}
}
xmal-
<ListBox
Name="lstCandidates"
ItemsSource="{Binding Path=UIProperties.CandidateAttributes}"
>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=DisplayName}">
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Control.ToolTip" Value="{Binding AttributePath.ConceptualPath}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
我可以用一些文本代替 Binding AttributePath.ConceptualPath,工具提示会显示该文本。只是想不通为什么它在绑定(bind)中不起作用。我怎样才能让它发挥作用?
最佳答案
您正在绑定(bind)到 AttributePath.ConceptualPath
但是AttributePath
返回 ObservableCollection<AttributeCollection>
而这个没有ConceptualPath
属性(property)。
您应该更改 AttributePath
的类型属性为CoBRRa_WPF.CoBRRaUtilities.ViewModels.QueryTool.AttributeCollection
或绑定(bind)到特定的 AttributeCollection
,例如第一个:
<Setter Property="Control.ToolTip" Value="{Binding AttributePath[0].ConceptualPath}"/>
还要确保 ConceptualPath
是 AttributeCollection
的公共(public)属性(property)类。
编辑:
如果你想在工具提示中显示路径列表,你应该使用 ItemsControl
:
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Control.ToolTip">
<Setter.Value>
<ItemsControl ItemsSource="{Binding AttributePath}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding ConceptualPath}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Setter.Value>
</Setter>
</Style>
关于c# - 样式设置的工具提示不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51788115/