WPF工具提示:是否有任何选项可以单击工具提示内的按钮(附带代码和图片)

时间:2015-09-03 20:29:36

标签: c# jquery .net wpf visual-studio

我正面临这个问题:

Visual explanation 代码:

<Grid>   
    <Button Content="Submit" Width="100" Height="100" Margin="10" VerticalAlignment="Top">
        <Button.ToolTip>
            <ToolTip Placement="MousePoint" StaysOpen="True" IsOpen="False" >
                <StackPanel Background="BlueViolet">
                    <TextBlock FontWeight="Bold">Submit Request</TextBlock>
                     <Button Name="btn2" Content="ClickMe" Margin="10" Click="btnRefresh_Click"/>
                </StackPanel>
            </ToolTip>
        </Button.ToolTip>
    </Button>
</Grid>

我也读过这个问题 WPF ToolTip containing buttons can not recieve Mouse events, alternative? 有没有解决方法?

1 个答案:

答案 0 :(得分:1)

使用Popup代替ToolTip

<Button Content="Open Tooltip" Width="120" Height="40" MouseEnter="ShowTooltip"/>
<Popup PopupAnimation="Fade" Placement="Mouse" AllowsTransparency="True" StaysOpen="False" x:Name="TooltipPopup" >
  <Border Background="AntiqueWhite" BorderBrush="Bisque" CornerRadius="3" BorderThickness="1">
    <StackPanel Margin="10" Orientation="Horizontal" >
      <Button Content="Update" HorizontalAlignment="Center" Click="UpdateTime" />
      <TextBlock HorizontalAlignment="Center" x:Name="TestClickTarget" Margin="20 0 0 0" VerticalAlignment="Center" />
    </StackPanel>
  </Border>
</Popup>

在代码隐藏中你会有这个(作为例子):

// opens the popup
// you can bind set TooltipPopup.IsOpen in any event you want (e.g. MouseEnter, Click, etc.
private void ShowTooltip(object sender, MouseEventArgs e){
    TooltipPopup.IsOpen = true;
}

// doing something, when the Button in popup got clicked:
private void UpdateTime(object sender, RoutedEventArgs e){
    TestClickTarget.Text = DateTime.Now.ToString("T");
}

<强>更新

使用TextBox

XAML MainWindow.xaml):

<Window x:Class="ClickableTooltip.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">
  <Grid>
    <TextBox MaxLines="10" MinLines="2" Height="100" Width="400" SelectionChanged="ShowTextTooltip" x:Name="Box">
    </TextBox>
    <Popup PopupAnimation="Fade" Placement="Mouse" AllowsTransparency="True" StaysOpen="False" x:Name="TooltipPopup" >
      <Border Background="AntiqueWhite" BorderBrush="Bisque" CornerRadius="3" BorderThickness="1"
              MinHeight="40" MaxHeight="100" MinWidth="200" MaxWidth="400">
        <StackPanel Margin="10" Orientation="Horizontal" >
          <Button Content="Update" HorizontalAlignment="Center" Click="UpdateTime" />
          <TextBlock HorizontalAlignment="Center" x:Name="TestClickTarget" Margin="20 0 0 0" VerticalAlignment="Center" />
        </StackPanel>
      </Border>
    </Popup>
  </Grid>
</Window>

C#MainWindow.xaml.cs):

public partial class MainWindow {
    public MainWindow() {
        InitializeComponent();
        TestClickTarget.Text = DateTime.Now.ToString("T");
        Box.AppendText("Hello SO users. I'm Javad Amiry." + Environment.NewLine);
        Box.AppendText("Hello SO users. I'm Javad Amiry in second line." + Environment.NewLine);
        Box.AppendText("Hello SO users. I'm Javad Amiry in third line ;)" + Environment.NewLine);
        Box.AppendText("And \"Hello SO users. I'm Javad Amiry.\" the last one :D" + Environment.NewLine);
    }

    private void ShowTextTooltip(object sender, RoutedEventArgs e) {
        var box = e.OriginalSource as TextBox;
        if (box == null || TestClickTarget == null)
            return;
        TestClickTarget.Text = box.SelectedText;
        TooltipPopup.IsOpen = box.SelectionLength != 0;
    }

    private void UpdateTime(object sender, RoutedEventArgs e) {
        TestClickTarget.Text = DateTime.Now.ToString("T");
    }
}