自定义控件,当点击自定义控件时弹出提示框
重要属性
属性 | 名称 | 解释 | 使用方法 |
---|---|---|---|
PlacementTarget | 安置目标 | Popup附着在那个控件上 | PlacementTarget="{Binding ElementName=TextBox1}" |
IsOpen | 是否显示 | 是否显示弹出框 | IsOpen="{Binding ElementName=TextBox1,Path=IsFocused, Mode=OneWay}" |
PopupAnimation | 弹出动画 | 显示窗口时是否使用动画,
只有在 AllowsTransparency 等于true时此属性才有用,设置一些Popup的弹出时的动画效果。
我们可以设置PopupAnimation=“Fade” 表示弹出时是通过渐入的方式进入的
| PopupAnimation=“Fade” //透明度渐变显现
PopupAnimation=“None”//触发显示
PopupAnimation=“Scroll”//左上角滑出
PopupAnimation=“Slide”//从上面滑出
|
HorizontalOffset | 水平偏移量 | 与附着控件水平偏移量 | |
VerticalOffset | 垂直偏移量 | 与附着控件的水平偏移量 | |
Placement | 弹出位置 | 设置弹出框相对于主体的位置 | Placement=“Top” |
AllowsTransparency | 允许背景为透明 | ||
StaysOpen | 控制关闭 | false 窗口移动,工具提示或弹出窗口Popup就会消失 |
普通使用
<Grid>
<TextBox x:Name="TextBox1" Grid.Column="1"/>
<Popup IsOpen="{Binding ElementName=TextBox1,Path=IsFocused, Mode=OneWay}" PlacementTarget="{Binding ElementName=TextBox1}" Height="70" Width="210" PopupAnimation="Slide" HorizontalOffset="10">
<Label Content="hahahha" Background="AliceBlue" VerticalAlignment="Center" HorizontalAlignment="Center" Height="70" Width="210"/>
</Popup>
</Grid>
自定义控件使用Popup
<Style x:Key="PopBox" TargetType="{x:Type TextBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid>
<TextBox x:Name="TextBox1" Height="30" Width="120"/>
<Popup IsOpen="{Binding ElementName=TextBox1,Path=IsFocused, Mode=OneWay}" PlacementTarget="{Binding ElementName=TextBox1}" Grid.ColumnSpan="2" Placement="Bottom" Height="60" Width="180">
<Label Height="60" Width=" 180" Background="AliceBlue" Content="{Binding ElementName=TextBox1,Path=Text}"/>
</Popup>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>