【问题标题】:Tooltip set by Style is not working样式设置的工具提示不起作用
【发布时间】:2019-01-18 03:56:34
【问题描述】:

以下列表框的工具提示是使用 setter 设置的。鼠标悬停时不会出现任何工具提示。

我怀疑问题出在列表框本身的 itemssource 上。列表框绑定到一个名为 CandidateAttributes 的 AttributeItems 列表。该列表的一个元素是一个名为 AttributePath 的 observablecollection,而我试图将工具提示绑定到的属性路径中的属性称为 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,工具提示会显示该文本。只是无法弄清楚为什么它在绑定中不起作用。我怎样才能让它工作?

【问题讨论】:

  • AttributePath 是一个集合。您要绑定此集合中的哪个项目?可能有几个。
  • ConceptualPath 是我想要绑定的。认为“Binding AttributePath.ConceptualPath”行会填充工具提示,因为 ConceptualPath 是 AttributePath 集合的属性。我忽略了包含来自 CoBRRa_WPF.CoBRRaUtilities.ViewModels.QueryTool.AttributeCollection 的 sn-p。会更新。
  • 不,attributePath 返回一个 ObservableCollection,而这个没有 ConceptualPath 属性。 AttributePath 是 AttributeCollections 的集合

标签: c# wpf data-binding


【解决方案1】:

您绑定到AttributePath.ConceptualPath,但AttributePath 返回一个ObservableCollection&lt;AttributeCollection&gt;,而这个没有ConceptualPath 属性。

您应该将AttributePath 属性的类型更改为仅CoBRRa_WPF.CoBRRaUtilities.ViewModels.QueryTool.AttributeCollection 或绑定到特定的AttributeCollection,例如第一个:

<Setter Property="Control.ToolTip" Value="{Binding AttributePath[0].ConceptualPath}"/>

还要确保 ConceptualPathAttributeCollection 类的公共属性。

编辑:

如果您想在工具提示中显示路径列表,您应该使用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>

【讨论】:

  • 这是有道理的。试过 AttributePath[0].ConceptualPath 但没用。
  • 我验证了 ConceptualPath 是 AttributeCollection 类的公共属性。
  • 那么是否填充了 AttributePath 集合?你调试发现了吗?
  • 已填充。事实上,我确实通过将它放在一个样式中,然后将 ItemsControl 的 ItemsSource 指定为 AttributePath 来部分工作。这种方法有其自身的问题,但确实填充了工具提示。
  • UIProperties.CandidateAttributes 返回什么?请提供您的问题的 MCVE:stackoverflow.com/help/mcve
猜你喜欢
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-14
  • 2011-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多