WPF:新的工具提示 Window

WPF: ToolTip in New Window

我有以下样式的工具提示:

        <Style x:Key="ToolTipPopUp" TargetType="{x:Type ContentControl}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ContentControl}">
                        <StackPanel Orientation="Horizontal">
                            <StackPanel.ToolTip>
                                <ToolTip Visibility="Hidden" />
                            </StackPanel.ToolTip>
                            <ContentPresenter />
                            <Image Source="/Resources/info.png" 
                                   ToolTip="{TemplateBinding ToolTip}"
                                   Height="12"
                                   VerticalAlignment="Top" />
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

这是通过以下方式调用的:

            <ContentControl Style="{StaticResource ToolTipPopUp}" ToolTip="Hello world">
                <CheckBox Content="I am a check box" />
            </ContentControl>

这允许我将 ToolTip 附加到我想要的任何控件并按预期显示 ToolTip

客户现在要求 ToolTip 在弹出窗口中显示 window 因为内容有时可能会很长。

有没有办法将此样式转换为显示弹出窗口window?我读到 Window 不能被 ContentControl 使用,所以如果有其他方法可以做到这一点,我对此持开放态度。

编辑

我有一个绑定到工具提示(这就是我现在正在做的)的指令的静态资源(为清楚起见未显示)。我正在考虑创建一个单例 window,当用户单击工具提示图标(蓝色 'i')时它会打开,会在弹出窗口 window 中更新 属性 (即,工具提示内容)每当单击工具提示图标时(因此,如果 window 已经打开,window 中的说明会更改)。然后用户可以在选择时关闭 window。

解决方案

感谢 Theodosius 为我指明了正确的方向。以下是我的实现方式:

MainPageViewModel.cs(执行程序的最顶层 window):

    private Window _toolTipWindow;

    /// <summary>
    /// Display the tool tip text in a new window
    /// </summary>
    public void ToolTipPopUpClicked(object sender, RoutedEventArgs e)
    {
        if (_toolTipWindow == null || !_toolTipWindow.IsVisible)
        {
            _toolTipWindow = new Window();
        }

        String _instructions = (((Button)sender).TemplatedParent as ContentControl).ToolTip.ToString();

        _toolTipWindow.Width = 300;
        _toolTipWindow.Height = 200;

        _toolTipWindow.Content = _instructions;
        _toolTipWindow.Show();
    }

StylesDictionary.xaml

<Style x:Key="ToolTipPopUp" TargetType="{x:Type ContentControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ContentControl}">
                <StackPanel Orientation="Horizontal">
                    <ContentPresenter />
                    <Button Width="Auto">
                        <i:Interaction.Triggers>
                            <i:EventTrigger EventName="Click">
                                <ei:CallMethodAction TargetObject="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}},
                                                                                        Path=DataContext}"
                                                             MethodName="ToolTipPopUpClicked" />
                            </i:EventTrigger>
                        </i:Interaction.Triggers>
                        <Button.Template>
                            <ControlTemplate>
                                <Image Source="Resources/info.png" 
                                            ToolTip="{TemplateBinding ToolTip}"
                                            Height="12"
                                            VerticalAlignment="Top" >

                                </Image>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

XAML 文件

            <ContentControl Name="PopupExample" 
                            Style="{StaticResource ToolTipPopUp}" 
                            ToolTip="Hello world">
                <CheckBox Content="I am a check box" />
            </ContentControl>

这保留了标准 ToolTip 功能,并在用户单击工具提示图标(蓝色 'i')时提供弹出窗口 window。

那这个呢?

<Style x:Key="ToolTipPopUp" TargetType="{x:Type ContentControl}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ContentControl}">
                    <StackPanel Orientation="Horizontal">
                        <StackPanel.ToolTip>
                            <ToolTip Visibility="Hidden" />
                        </StackPanel.ToolTip>
                        <ContentPresenter />
                        <Image Source="/info.png" 
                               ToolTip="{TemplateBinding ToolTip}"
                               Height="12"
                               VerticalAlignment="Top" >
                            <Image.Style>
                                <Style>
                                    <EventSetter Event="Mouse.MouseEnter" Handler="Show_PopupToolTip" />
                                    <EventSetter Event="Mouse.MouseLeave" Handler="Hide_PopupToolTip"/>
                                </Style>
                            </Image.Style>
                        </Image>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

代码隐藏...

Window win;

    private void Show_PopupToolTip(object sender, MouseEventArgs e)
    {
        var fred = ((Image)sender).TemplatedParent;

        if (fred != null)
        {
            ContentControl c = fred as ContentControl;
            string myText = c.ToolTip.ToString();

            if (!String.IsNullOrEmpty(myText))
            {
                if (win == null || !win.IsVisible)
                    win = new Window();
                win.Height = 275;
                win.Width = 275;
                win.Content = myText;
                win.Show();
            }
        }
    }
    private void Hide_PopupToolTip(object sender, MouseEventArgs e)
    {
        //if (win != null)
        //{
        //    win.Close();
        //}
    }