WPF 工具提示:是否有任何选项可以单击工具提示内的按钮(代码和图片附后)
WPF ToolTip: Is there any option to Click on a Button that is inside tooltip (code and picture attached)
我面临这个问题:
代码:
<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?
有什么解决方法吗?
使用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");
}
}
我面临这个问题:
<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? 有什么解决方法吗?
使用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");
}
}