简体   繁体   English

如何为 TreeView 中的 ToolTip 提供 MaxWidth(或自定义样式)

[英]How to give MaxWidth (or custom styling) to the ToolTip in a TreeView

I have a ResourceDictionary that has a TreeView.我有一个具有 TreeView 的 ResourceDictionary。

Here, I am trying to give a MaxWidth to my MainItemStyle's tooltip (as my bound text 'TxtDescription' is very long), and for doing that i tried to give a template to this tooltip but it got applied to the base TreeViewItem instead.在这里,我试图为我的 MainItemStyle 的工具提示提供一个 MaxWidth(因为我的绑定文本“TxtDescription”很长),为此我试图为这个工具提示提供一个模板,但它被应用于基础 TreeViewItem。

Additionally, I am trying to set tooltip to the TreeView header items only, but i think that is not possible with a TreeView's structure.此外,我试图仅将工具提示设置为 TreeView 标题项,但我认为这对于 TreeView 的结构是不可能的。

My code is like this:我的代码是这样的:

<ResourceDictionary>
<TreeView ItemsSource="{Binding MainItems, UpdateSourceTrigger=PropertyChanged}"
          ItemContainerStyle="{StaticResource MainItemStyle}">
        <TreeView.ItemTemplate>
          <HierarchicalDataTemplate ItemsSource="{Binding ChildList}" ItemTemplate="{StaticResource childTemplate}" ItemContainerStyle="{StaticResource ChildItemStyle}">           
          </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>        
</TreeView>

<Style x:Key="basicStyle" TargetType="TreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
    <Setter Property="Margin" Value="0"/>
    <Setter Property="Padding" Value="2,2"/>    
    <Setter Property="SnapsToDevicePixels" Value="True"/>    
  </Style>

  <Style x:Key="ChildItemStyle" TargetType="TreeViewItem" BasedOn="{StaticResource basicStyle}">    
  </Style>

  <Style x:Key="MainItemStyle" TargetType="TreeViewItem" BasedOn="{StaticResource basicStyle}">
    <Setter Property="ToolTip" Value="{Binding TxtDescription}"/>    
  </Style>
</ResourceDictionary>

Can anyone please provide me any solution for the tooltip's MaxWidth (or styling) here.任何人都可以在这里为我提供工具提示的 MaxWidth(或样式)的任何解决方案。 Thanks a ton.万分感谢。

Just assign a ToolTip control to the TreeViewItem.ToolTip property and customize it:只需将ToolTip控件分配给TreeViewItem.ToolTip属性并对其进行自定义:

<Style x:Key="MainItemStyle" TargetType="TreeViewItem" BasedOn="{StaticResource basicStyle}">
  <Setter Property="ToolTip">
    <Setter.Value>
      <ToolTip Content="{Binding TxtDescription}" 
               MaxWidth="500" />    
    </Setter.Value>
  </Setter>
</Style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM