首先我想做什么:
我有一个Popup
(显示wenn ALT键已关闭并在向上时隐藏),将其设为Border
并在其中TextBlock
。这将显示特定字段的键绑定。
<Popup PlacementTarget="{Binding ElementName=txb_ticket_Text}" IsOpen="{Binding ShowTooltip}"
Style="{DynamicResource TooltipPopup}" >
<Border Style="{DynamicResource WhiteBorder}">
<TextBlock Text="{Binding ElementName=kb_ticketselection, Path=Key, Converter={StaticResource KeyToString}}"
Style="{DynamicResource Tooltip}"/>
</Border>
</Popup>
遵循三种风格:
<Style x:Key="Tooltip" TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="Black" />
<Setter Property="Foreground" Value="White" />
<Setter Property="FontSize" Value="20" />
<Setter Property="Width" Value="30" />
<Setter Property="Height" Value="30" />
<Setter Property="TextAlignment" Value="Center" />
</Style>
<Style x:Key="WhiteBorder" TargetType="{x:Type Border}">
<Setter Property="BorderBrush" Value="White" />
<Setter Property="BorderThickness" Value="1" />
</Style>
<Style x:Key="TooltipPopup" TargetType="{x:Type Popup}">
<Setter Property="Placement" Value="Left" />
<Setter Property="VerticalOffset" Value="-10" />
</Style>
是否可以制作样式或模板?当然,我有多次使用它,最简单的方法就是设置Textbinding。这三个元素都没有模板。
我的下一个想法是创建一个包含这些元素的UserControl,并设置绑定到Textblock的textproperty?!
€dit:同时我创建了一个UserControl,但现在我遇到了一个简单的问题:
我收到以下错误,但仍然有效...
&#34; TargetType&#39; StandInPopup&#39;&#34; entspricht nicht dem Typ des Elements&#34; Popup&#34;。
&#34; TargetType&#39; StandInPopup&#39;&#34;与元素类型不匹配&#34;弹出&#34;。
如何删除错误?或是什么导致了这个错误,为什么它仍然有效(如预期的那样)?
UC供参考
<UserControl x:Class="PopupTooltip"
x:Name="ucpopup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:myClass"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Popup PlacementTarget="{Binding ElementName=ucpopup, Path=TargetElement}" IsOpen="{Binding ShowTooltip}"
Style="{DynamicResource TooltipPopup}" >
<Border Style="{DynamicResource WhiteBorder}">
<TextBlock Text="{Binding ElementName=ucpopup, Path=MyText}"
Style="{DynamicResource Tooltip}"/>
</Border>
</Popup>
</Grid>
</UserControl>
€:这是UC的其余代码,只需要两个属性:
Public Class PopupTooltip
Public Sub New()
' Dieser Aufruf ist für den Designer erforderlich.
InitializeComponent()
' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.
End Sub
Private _myText As String
Public Property MyText() As String
Get
Return _myText
End Get
Set(ByVal value As String)
_myText = value
End Set
End Property
Public Shared ReadOnly MyTextProperty As DependencyProperty = DependencyProperty.Register("MyText", GetType(String), GetType(PopupTooltip), New PropertyMetadata(""))
Private _targetElement As UIElement
Public Property TargetElement() As UIElement
Get
Return _targetElement
End Get
Set(ByVal value As UIElement)
_targetElement = value
End Set
End Property
Public Shared ReadOnly TargetElementProperty As DependencyProperty = DependencyProperty.Register("TargetElement", GetType(UIElement), GetType(PopupTooltip))
End Class