0

这不是我想要的那么难,但我几天都在拉我的头发!

我只想要与 WINdows Explorer 相同的工具提示行为:使用显示完整元素的工具提示覆盖部分隐藏的树/列表元素

我在树视图中使用以下数据模板

<HierarchicalDataTemplate DataType="{x:Type TreeVM:SurveyorTreeViewItemViewModel}" ItemsSource="{Binding Children, Converter={StaticResource surveyorSortableCollectionViewConverter}}">
    <StackPanel x:Name="SurveyorStackPanel" Margin="0" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Orientation="Horizontal" Height="20" Width="auto">
      ... (Textblocks, properties, usercontrol, border,... )                   
      <StackPanel.ToolTip>
          <ToolTip Placement="RelativePoint" Padding="0" HasDropShadow="False" 
               DataContext="{Binding ElementName=SurveyorStackPanel}">
                <Rectangle HorizontalAlignment="Left" VerticalAlignment="Center"
                           Width="{Binding ElementName=SurveyorStackPanel, Path=Width}"
                           Height="{Binding ElementName=SurveyorStackPanel, Path=Height}">
                     <Rectangle.Fill>
                        <VisualBrush AutoLayoutContent="True" AlignmentX="Left" 
                                     Visual="{Binding}" Stretch="None"/>
                     </Rectangle.Fill>
                </Rectangle>
           </ToolTip>
       </StackPanel.ToolTip>                                  
   </StackPanel>
</HierarchicalDataTemplate>

如您所见,我正在尝试使用 Visualbrush。但这不起作用。它只显示您在屏幕上看到的内容。

我已经尝试使用静态资源并绑定在工具提示中的新堆栈面板上,但这只会留下一个 blanc 工具提示。

我有什么问题吗?我必须使用替代品吗?我在 WPF 中很新。我知道基础知识,但绑定/资源对我来说有点新

编辑这里是我试过的静态源:

<ToolTip x:Key="reflectingTooltip" DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}" Placement="RelativePoint" Padding="0" HasDropShadow="False">
   <Rectangle Width="{Binding ActualWidth}" Height="{Binding Path=ActualHeight}" Margin="0"
              HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
      <Rectangle.Fill>
           <VisualBrush Visual="{Binding}" Stretch="None" AlignmentX="Left" />
      </Rectangle.Fill>
   </Rectangle>
</ToolTip>

编辑 2

以下是我现在的情况的几张图片:当工具提示显示时,必须显示整个元素。在工具提示之前:http ://desmond.imageshack.us/Himg832/scaled.php?server=832&filename=beforedo.png&res=landing

当显示工具提示时:http ://desmond.imageshack.us/Himg842/scaled.php?server=842&filename=afterbl.png&res=landing

工具提示的高度太大,只显示屏幕显示的内容。唯一的问题是“填写”隐藏的文本。

4

1 回答 1

0

VisualBrush 以位图的形式呈现与您通过“Visual”属性提供的完全相同的内容,并且它无需对该内容进行任何修改即可:它完全按照现在的方式呈现它们。

如果您想显示其他内容,则必须提供其他内容。您可以尝试使用类似的内容吗:?

<Window x:Class="UncutTooltip.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Horizontal">
        <ListBox ItemsSource="{Binding}">
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                    <Setter Property="Width" Value="250" />
                </Style>
            </ListBox.ItemContainerStyle>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid Background="Transparent">
                        <TextBlock Text="{Binding TheText}"
                            TextTrimming="CharacterEllipsis">
                        </TextBlock>

                        <Grid.ToolTip>
                            <TextBlock Text="{Binding TheText}"
                            TextTrimming="CharacterEllipsis">
                            </TextBlock>
                        </Grid.ToolTip>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <Border Background="Red" >
            <TextBlock Margin="5" Foreground="WhiteSmoke" FontSize="18"
                       Text="The end of window:)" TextAlignment="Center">
                <TextBlock.LayoutTransform>
                    <RotateTransform Angle="-90" />
                </TextBlock.LayoutTransform>
            </TextBlock>
        </Border>
    </StackPanel>
</Window>

---

using System.Collections.Generic;
using System.Windows;
namespace UncutTooltip
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new List<Item>
            {
                new Item { TheText = "its not that hard what i want, but i'm pulling my hairs for days!" },
                new Item { TheText = "i just want the same tooltip behaviour like the WIndows Explorer: overlay a partially hidden tree/list element with the tooltip that displays the full element" },
                new Item { TheText = "i use the following datatemplate in my treeview" },
                new Item { TheText = "As you can see, i'm trying to use Visualbrush. but this doesnt work. it only shows what you see on the screen." },
                new Item { TheText = "I have tried with static resource and binding on a new stackpanel thats in the tooltip, but that only leaves with a blanc tooltip." },
                new Item { TheText = "Do i something wrong? do i have to use alternatives? i'm pretty new in WPF. i know the basics, but binding/resources is kinda new for me" },
            };
        }
    }

    public class Item
    {
        public string TheText { get; set; }
    }
}

编辑:

现在,将工具提示内容更改为 ie:

                    <Grid.ToolTip>
                        <ListBox ItemsSource="{Binding TheWholeList}">
                            <ListBox.ItemContainerStyle>
                                <Style TargetType="ListBoxItem">
                                    <Setter Property="HorizontalAlignment" Value="Stretch" />
                                    <!--<Setter Property="Width" Value="250" />-->
                                </Style>
                            </ListBox.ItemContainerStyle>
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <Grid Background="Transparent">
                                        <TextBlock Text="{Binding TheText}" />
                                    </Grid>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </Grid.ToolTip>

并将数据定义更改为:

public class Item
{
    public string TheText { get; set; }
    public IList<Item> TheWholeList { get; set; }
}

        var tmp = new List<Item>
        {
             .........
        };

        foreach (var it in tmp)
            it.TheWholeList = tmp;

        this.DataContext = tmp;

请注意,我已经注释掉了工具提示列表框中的宽度约束,它将显示一个未截断元素的未截断列表。

编辑#2:

<StackPanel Orientation="Horizontal">
    <ListBox x:Name="listbox" ItemsSource="{DynamicResource blah}">   // <---- HERE
        <ListBox.ItemContainerStyle>
            <Style TargetType="ListBoxItem">
                <Setter Property="HorizontalAlignment" Value="Stretch" />
                <Setter Property="Width" Value="250" />
            </Style>
        </ListBox.ItemContainerStyle>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Background="Transparent">
                    <TextBlock Text="{Binding TheText}" TextTrimming="CharacterEllipsis" />

                    <Grid.ToolTip>
                        <ToolTip DataContext="{DynamicResource blah}">   // <---- HERE
                            <TextBlock Text="{Binding [2].TheText}" />   // <---- just example of binding to a one specific item
                            <!-- <ListBox ItemsSource="{Binding}">  another eaxmple: bind to whole list.. -->
                        </ToolTip>
                    </Grid.ToolTip>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>


public class Item
{
    public string TheText { get; set; }
}

    public MainWindow()
    {
        InitializeComponent();

        Resources["blah"] = new List<Item>   // <---- HERE
        {
            new Item { TheText = ........
             ........

在最后一个示例中,我将 window.DataContext 绑定更改为与 DynamicResource 的绑定。在窗口初始化中,我还更改了数据传递到窗口的方式。我已更改工具提示模板以明确包含工具提示,并将其绑定到同一资源。这样,内部工具提示的文本块能够直接读取数据源的第三行 - 这证明它绑定到列表,而不是项目。

然而,这是蹩脚的方法。它仅适用于显式工具提示,仅适用于 Tooltip.DataContext=resource,并且可能是这种方法的唯一工作形式。可能有可能侵入带有附加行为的工具提示并搜索它的父窗口和让绑定工作,但通常不值得。您可以尝试绑定到第二个示例中的项目属性吗?

于 2012-08-22T08:08:07.263 回答