gpt4 book ai didi

wpf - 以编程方式显示工具提示

转载 作者:行者123 更新时间:2023-12-04 10:51:12 24 4
gpt4 key购买 nike

我开发了一个 WPF 示例项目。

这是主窗口的 XAML 标记:

<Window x:Class="ToolTipSample.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"
WindowState="Maximized">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>

<Button Click="OnButtonClick">Show ToolTip</Button>

<StatusBar Grid.Row="2">
<StatusBarItem>
<TextBlock Text="TextBlock With ToolTip">
<TextBlock.ToolTip>
<ToolTip x:Name="m_toolTip">
ToolTip
</ToolTip>
</TextBlock.ToolTip>
</TextBlock>
</StatusBarItem>
</StatusBar>
</Grid>
</Window>

这是没有 using 语句的主窗口的代码隐藏:
namespace ToolTipSample
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void OnButtonClick(object p_sender, RoutedEventArgs p_args)
{
m_toolTip.IsOpen = true;
}
}
}

我想在单击按钮时以编程方式显示工具提示。
我希望 ToolTip 显示在其 TextBlock 父级上方。

当鼠标光标在 TextBlock 上并且在大约等于 5 秒的恒定时间 C 期间,将自动显示工具提示。
我希望在单击按钮时在 C 期间显示工具提示。

我的目标在当前项目中没有实现。
单击按钮时会显示工具提示:

enter image description here

但 :
  • ToolTip 离它的 TextBlock 父级太远。
  • 工具提示不会自动隐藏

  • 我必须做什么才能实现我的目标?
    任何帮助将不胜感激。

    最佳答案

    你有很多问题。第一个也是最简单的解决方法是您只打开而不是关闭 ToolTip .您说我希望在单击按钮时的相同时间内显示工具提示,这很容易实现处理 PreviewMouseDownPreviewMouseUp事件:

    private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
    m_toolTip.PlacementTarget = PlacementTarget;
    m_toolTip.IsOpen = true;
    }

    private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
    m_toolTip.IsOpen = false;
    }

    ...
    <Grid>
    <Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="*" />
    <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <Button PreviewMouseDown="Button_PreviewMouseDown"
    PreviewMouseUp="Button_PreviewMouseUp">Show ToolTip</Button>
    <StatusBar Grid.Row="2">
    <StatusBarItem>
    <TextBlock Name="PlacementTarget" Text="TextBlock With ToolTip">
    <TextBlock.ToolTip>
    <ToolTip x:Name="m_toolTip" Placement="Top" HorizontalOffset="50"
    VerticalOffset="-5">ToolTip</ToolTip>
    </TextBlock.ToolTip>
    </TextBlock>
    </StatusBarItem>
    </StatusBar>
    </Grid>

    你的另一个问题有点难以解决......在我看来,你的 ToolTip 的定位可能涉及一些错误。 .通常,尽管@icebat 说了些什么,但可以更改 ToolTip 的位置使用 ToolTip.Placement property .这可以设置为 PlacementMode Enumerations 之一.

    默认值为 Mouse这是 MSDN 上链接页面的定义:

    A postion of the Popup control that aligns its upper edge with the lower edge of the bounding box of the mouse and aligns its left edge with the left edge of the bounding box of the mouse. If the lower screen-edge obscures the Popup, it repositions itself to align with the upper edge of the bounding box of the mouse. If the upper screen-edge obscures the Popup, the control repositions itself to align with the upper screen-edge.



    这解释了为什么 ToolTip显示远离 TextBlock展示位置目标...因为 Button因此鼠标(点击时)远离 TextBlock .但是,通过设置 Placement属性设置为另一个值应该能够实现广泛的位置。但是,设置 Placement的不同值单独的属性,只会显示 ToolTip在屏幕的左上角。

    要解决这种情况,您还应该设置 ToolTip.PlacementTarget Property ,正如@icebat 在评论中正确指出的那样,但显然仅来自代码。曾经 PlacementTarget属性已设置, Placement属性值按预期工作。从这个链接页面:

    You can position a ToolTip by setting the PlacementTarget, PlacementRectangle, Placement, HorizontalOffset, and VerticalOffsetProperty properties.



    enter image description here

    关于wpf - 以编程方式显示工具提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23673881/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com