您的位置:首页 > 其它

#377 – 通过将ToolTip定义为资源对其进行重用(Reuse Tooltips by Defining Them as Resources)

2016-10-27 11:26 387 查看
因为提示工具是一个Tooltip 控件的实例,隐藏你可以通过资源字典创建提示控件并重用他们。

下面是一个用绑定数据显示提示控件的例子:

首先定义资源,

<Window.Resources>
<!-- Standard tooltip for TextBox controls, displays Text property of parent control in a TextBlock -->
<ToolTip x:Key="textBlockTooltip" DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}">
<StackPanel>
<Label FontWeight="Bold" Content="Full Text"/>
<TextBlock Text="{Binding Text}" TextWrapping="Wrap" Width="200"/>
</StackPanel>
</ToolTip>
</Window.Resources>

然后我们在TextBox 中使用它,

<TextBox Text="Now is the winter of our discontent etc"
Width="100" Margin="10" ToolTip="{StaticResource textBlockTooltip}"/>
<TextBox Text="All the world's a stage etc"
Width="100" Margin="10" ToolTip="{StaticResource textBlockTooltip}"/>

下面是效果图:









使用同样的代码创建提示信息,但是每个Textbox的提示信息都是各自的。

原文地址:https://wpf.2000things.com/2011/09/01/377-reuse-tooltips-by-defining-them-as-resources/


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  WPF tooltip
相关文章推荐