c# js popup_关于WPF中Popup中的一些用法的总结

Popup控件是一个常用的非常有用的控件,顾明思义就是弹出式控件,首先我们来看看MSDN对它的解释吧,表示具有内容的弹出窗口,这个是非常重要的控件,我们看看它的继承关系吧:

Popup控件是从FrameworkElement直接继承而来的,属于非常高的层级,我们在使用中使用的最多的属性就是下面这些属性:1 PlacementTarget 表示Popup控件的放置的位置依赖的对象,这个通常使用绑定的方式来标明Popup控件停靠的目标。比如说:PlacementTarget="{Binding ElementName=PCheckBox}"  表示Popup停靠的位置依赖于一个名为PCheckBox的ChenkBox对象,这个也是经常使用的一种情况,我们可以将Popup控件和CheckBox,ToggleButton等一系列的控件来配合使用作出不同的效果。2 Placement属性:获取或设置的方向 Popup 控件时,控件将打开,并指定的行为 Popup 控制时与屏幕边界重叠。MSDN上面的解释是:您可以通过设置相关的属性来定位弹出的位置,通过设置 PlacementTarget、PlacementRectangle、Placement、HorizontalOffset 和 VerticalOffsetProperty 属性来定位弹出项。3 其实这里PlacementRectangle和HorizontalOffset 和 VerticalOffsetProperty这一对属性可以做一些等价的替换,这些都是可以对Popup的弹出的位置进行微调。4 IsOpen属性,这个是最重要的属性之一,通常是通过绑定的方式来为其进行赋值,比如说:IsOpen="{Binding ElementName=PCheckBox,Path=IsChecked}" 是通过绑定CheckBox的IsChecked属性来控制Popup的弹出。最后需要重点介绍的就是StayOpen属性,MSDN的解释是:获取或设置一个值,该值指示当 Popup 控件焦点不再对准时,是否关闭该控件。当将 StaysOpen 属性设为 true 时,Popup 始终处于打开状态,直到通过将 IsOpen 属性设置为 false 将其显式关闭。当 StaysOpen 设置为false 时,Popup 控件会截获所有鼠标事件和键盘事件,以确定在 Popup 控件之外发生这些事件之一,最明显的区别是当设置IsOpen 为True时弹出Popup控件,当使用鼠标在另外的地方进行点击时Popup失去焦点,同时Popup隐藏,而当StaysOpen 设置为True时,当Popup失去焦点时,Popup则不会隐藏,此时仍然会保持打开的状态。

还有我们还可以设置一些Popup的弹出时的动画效果。我们可以设置PopupAnimation="Fade" 表示弹出时是通过渐入的方式进入的,这些在使用时需要注意。

下面通过一个小例子来说明Popup的用法,通过TextBox和Popup配合使用来达到类似于百度搜索框的效果,首先贴出重点的实现代码:

Text="{Binding DutyPersonName,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"

Width="70"

Tag="{Binding DataContext,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ItemsControl}}">

CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBox}}">

CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=TextBox}}">

Width="{Binding ActualWidth,ElementName=dutyPersonTextBox}"

IsOpen="{Binding ElementName=dutyPersonTextBox,Path=IsKeyboardFocused, Mode=OneWay}"

StaysOpen="True">

SnapsToDevicePixels="true"

ItemsSource="{Binding DataContext.SpecificHistoryMembers,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=my:MainWindow},Mode=TwoWay}"

HorizontalAlignment="Stretch"

ScrollViewer.HorizontalScrollBarVisibility="Disabled"

Background="#fff">

CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}">

Height="Auto"

Width="Auto"

BorderBrush="{TemplateBinding BorderBrush}"

BorderThickness="{TemplateBinding BorderThickness}"

Background="{TemplateBinding Background}"

Padding="1"

SnapsToDevicePixels="true">

SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"

VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>

Value="false">

Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>

IsItemsHost="True"

HorizontalAlignment="Left"

VerticalAlignment="Center"

Width="{Binding ActualWidth,ElementName=dutyPersonTextBox}">

Padding="2"

SnapsToDevicePixels="true"

BorderThickness="1">

HorizontalAlignment="Stretch"

HorizontalContentAlignment="Left"

FontSize="13">

Value="true">

Value="#00a3d9"

TargetName="Border">

Value="0.6"

TargetName="Border">

最终实现的效果,如下所示:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值