WPF:根据光标显示并保留文本框的工具提示 [英] WPF: Show and persist ToolTip for a Textbox based on the cursor
问题描述
此工具提示的目的是显示必须输入的字符串的格式.
The purpose of this tooltip is to show, the format of the string which must be entered.
我想要实现的功能是:
- 当用户将光标放在文本框中时,即当用户将其标签到控件中时,应显示工具提示.
- 工具提示应根据用户在文本框中的输入进行更新(这可以通过绑定来实现).
- 工具提示必须一直存在,直到用户退出控件为止.
我想知道所提供的标准工具提示是否具有可用于实现此目的的配置设置,属性,...到目前为止,在我的研究中我没有发现任何设置.如果现有的工具提示不适合该任务(很可能是这样),我想要一些指针,示例代码来实现此目标...
I wanted to know if the standard tooltip as provided has configuration settings, properties, that can be used to achieve this,... in my research thus far I haven't found any. If the existing tooltip is not up to the task, which is very likely, I'd like some pointers, sample code to achieve this...
谢谢
Hasanain
推荐答案
结合使用事件触发器,绑定和最少的代码隐藏,我设法实现了一种行为,该行为将在用户在文本框中键入内容时更新ToolTip.当键盘焦点丢失时,工具提示会消失.
Using a combination of event triggers, bindings, and minimal code-behind I managed to implement a behavior which would update the ToolTip while the user types into textbox; when the keyboard focus is lost the tooltip disappears.
这是文本框的xaml:
Here is the xaml for the textbox:
<TextBox Grid.Column="0" x:Name="txtBxQckTkt" Margin="5,5,0,0" Width="250" ToolTipService.IsEnabled="True"
Text="{Binding QuickTicketText}">
<TextBox.Triggers>
<EventTrigger RoutedEvent="TextBox.GotKeyboardFocus">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames
Storyboard.TargetName="txtBxQckTktToolTip"
Storyboard.TargetProperty="IsOpen">
<DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="False"/>
<DiscreteBooleanKeyFrame KeyTime="0:0:0.0001" Value="True" />
</BooleanAnimationUsingKeyFrames>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="txtBxQckTktToolTip"
Storyboard.TargetProperty="Placement">
<DiscreteObjectKeyFrame Value="{x:Static PlacementMode.Bottom}"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="TextBox.LostKeyboardFocus">
<BeginStoryboard>
<Storyboard>
<BooleanAnimationUsingKeyFrames
Storyboard.TargetName="txtBxQckTktToolTip"
Storyboard.TargetProperty="IsOpen">
<DiscreteBooleanKeyFrame KeyTime="0:0:0" Value="True"/>
<DiscreteBooleanKeyFrame KeyTime="0:0:0.0001" Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBox.Triggers>
<TextBox.ToolTip>
<ToolTip x:Name="txtBxQckTktToolTip" Placement="Bottom" Content="{Binding ToolTip}">
</ToolTip>
</TextBox.ToolTip>
</TextBox>
这是背后的代码:
txtBxQckTktToolTip.PlacementTarget = txtBxQckTkt;
_handler = (s, e) =>
{
var viewModel = DataContext as SingleTradeEntryViewModel;
if (viewModel == null) return;
viewModel.OnKeyup.Execute(txtBxQckTkt.Text);
};
txtBxQckTkt.KeyUp -= _handler;
txtBxQckTkt.KeyUp += _handler;
执行命令(OnKeyup)时,它会为绑定的ToolTip属性引发更改通知,如xaml中所示.
When the command (OnKeyup) executes, it raises a change notification for the ToolTip property bound as seen in the xaml.
谢谢
Hasanain
这篇关于WPF:根据光标显示并保留文本框的工具提示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!