简体   繁体   English

在ToolTip WPF上使用情节提要

[英]Using a Storyboard on a ToolTip WPF

I am trying to create a storyboard, so when a user clicks on a textbox, its copies the text to their clipboard and shows a tooltip saying copied, which then fades away. 我正在尝试创建一个情节提要板,以便当用户单击文本框时,它会将文本复制到他们的剪贴板中并显示提示复制的工具提示,然后该提示消失。

Here is my attempt: 这是我的尝试:

xaml : xaml

<TextBox Name="PolyValue" Text="{Binding .}" IsReadOnly="True" BorderThickness="0" Background="White"
        VerticalAlignment="Center" PreviewMouseDown="CopyTextBox" >

    <TextBox.ToolTip>
        <ToolTip Style="{StaticResource TooltipPopupFadeAway}" IsOpen="True" Opacity="0" Background="Transparent" BorderThickness="0">
            <Border Background="White" BorderBrush="Black" BorderThickness="1" CornerRadius="3" >
                <Label Content="Copied" Padding="5, 2" />
            </Border>
        </ToolTip>
    </TextBox.ToolTip>
</TextBox>

Here is the Storyboard : 这是Storyboard

<Style x:Key="TooltipPopupFadeAway" TargetType="ToolTip">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Opacity, RelativeSource={RelativeSource Self}}" Value="1">
            <DataTrigger.EnterActions>
                <BeginStoryboard x:Name="ClosePopupStoryBoard">
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity" Duration="00:00:01" From="3" To="0" />
                    </Storyboard>
                </BeginStoryboard>
            </DataTrigger.EnterActions>
        </DataTrigger>
    </Style.Triggers>
</Style>

Code behind: 后面的代码:

private void CopyTextBox(object sender, MouseButtonEventArgs e)
{
    if(sender is TextBox textBox)
    {
        Clipboard.SetText(textBox.Text);
        (textBox.ToolTip as ToolTip).IsOpen = false;
        (textBox.ToolTip as ToolTip).IsOpen = true; //this recalculates the position
        (textBox.ToolTip as ToolTip).Opacity = 1;

    }
}

This works exactly how I want it to, however it only works once, after it has been shown and the user clicks again nothing happens. 这完全符合我的要求,但是仅显示一次,并且用户再次单击后,它什么也没有发生。

After setting a break point in the CopyTextBox method, the of the ToolTip opacity is 0 even after programmatically setting it to 1. CopyTextBox方法中设置断点后,即使在通过编程将其设置为1后,ToolTip的不透明度也为0。

I am not sure what I am doing wrong? 我不确定自己在做什么错?

You should trigger directly on the IsOpen property. 您应该直接在IsOpen属性上触发。 Make sure to set the default Opacity to 0 , and do not explicitly set it to 1 afterwards. 确保将默认的Opacity设置为0 ,然后不要将其显式设置为1

<Style x:Key="TooltipPopupFadeAway" TargetType="ToolTip">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="BorderThickness" Value="0"/>
    <Setter Property="Opacity" Value="0"/>
    <Setter Property="IsOpen" Value="False"/>
    <Style.Triggers>
        <Trigger Property="IsOpen" Value="True">
            <Trigger.EnterActions>
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="Opacity"
                                         Duration="0:0:1" From="3" To="0"/>
                    </Storyboard>
                </BeginStoryboard>
            </Trigger.EnterActions>
        </Trigger>
    </Style.Triggers>
</Style>

In code behind, do not to set the Opacity: 在后面的代码中,不要设置不透明度:

private void CopyTextBox(object sender, MouseButtonEventArgs e)
{
    if (sender is TextBox textBox)
    {
        Clipboard.SetText(textBox.Text);
        ((ToolTip)textBox.ToolTip).IsOpen = false;
        ((ToolTip)textBox.ToolTip).IsOpen = true;
    }
}

This is the sort of approach I mean. 我的意思是这种方法。

Just binding the text property means you need no code. 只需绑定text属性就意味着您不需要任何代码。

I'm not sure this does exactly what you want because you seem to have previewmousedown showing the tooltip. 我不确定这是否确实符合您的要求,因为您似乎已经在预览时显示了工具提示。 Which is a bit odd for a tooltip since mouseover shows them. 对于工具提示,这有点奇怪,因为鼠标悬停显示了它们。

<Window.Resources>
    <ControlTemplate x:Key="TooltipPopupFadeAway" TargetType="ToolTip">
                    <Border Background="Yellow">
                    <TextBlock Text="{Binding PlacementTarget.Text, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"
                               Name="TheText"
                               />
                    </Border>
        <ControlTemplate.Triggers>
            <EventTrigger RoutedEvent="ToolTip.Opened">
                <BeginStoryboard>
                    <Storyboard TargetProperty="Opacity">
                        <DoubleAnimation From="1.0" To="0" Duration="0:0:2" />
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </ControlTemplate.Triggers>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <TextBox>
        <TextBox.ToolTip>
            <ToolTip Template="{StaticResource TooltipPopupFadeAway}"/>
        </TextBox.ToolTip> 
    </TextBox>
</Grid>

And you could set that template and tooltip etc via style if it suited you better. 如果您更适合,可以通过样式设置模板和工具提示等。 I used a yellow background so I can see it easy. 我使用了黄色背景,所以我看起来很容易。 The tooltip probably doesn't exactly match what you had. 工具提示可能与您所拥有的不完全匹配。

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

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