c# - 样式设置的工具提示不起作用

标签 c# wpf data-binding

下面列表框的工具提示是使用 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}"/>

还要确保 ConceptualPathAttributeCollection 的公共(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/

相关文章:

c# - 动态编译以提高性能

C# - 带有 linq 参数的泛型方法

c# - 创建 Windows 窗体 打开新窗体

wpf - 根据 TreeView 选择绑定(bind)到的类型显示用户控件

WPF 绑定(bind)和资源查找复杂性

data-binding - groovy swingbuilder 可绑定(bind)列表和表格

c# - .NET 反编译器区分 "using"和 "try...finally"

c# - DispatcherOperation.Task 立即返回

wpf - 我只需要一个显示多行的简单 WPF ToolTip 样式

android - 使用数据绑定(bind)时从 getTimeInMillis 获取日期