一、Popup控件的主要属性

Popup表示具有内容的弹出窗口,其主要属性为:

  1. Child:获取或设置 Popup控件的内容。
  2. IsOpen:获取或设置一个值,该值指示Popup 是否可见
  3. Placement:获取或设置 Popup 控件打开时的控件方向,并指定Popup 控件在与屏幕边界重叠时的控件行为
  4. PlacementTarget:获取或设置当打开 Popup 控件时该控件相对于其放置的元素。
  5. PopupAnimation:获取或设置Popup 控件的打开和关闭动画。
  6. StaysOpen:获取或设置一个值,该值指示当 Popup 控件焦点不再对准时,是否关闭该控件。

Popup主要事件为:

  1. Opened:当IsOpen 属性更改为 true 时发生。

    private void PopupOpening(object sender, EventArgs e)
    {
    //Code to execute when Popup opens
    }
  2. Closed:当IsOpen 属性更改为 false 时发生。

    private void PopupClosing(object sender, EventArgs e)
    {
    //Code to execute when Popup closes
    }

二、Popup控件使用

我们使用Popup实现一个功能:当文本框获取到焦点时,弹出下拉框,里面选择水果种类,具体效果如下图所示:

相关的界面代码为:

  <Window.Resources>
<x:Array x:Key="MyArray" Type="system1:String">
<system1:String>西瓜</system1:String>
<system1:String>葡萄</system1:String>
<system1:String>芒果</system1:String>
<system1:String>猕猴桃</system1:String>
</x:Array>
</Window.Resources>
<Grid Margin="20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0" Height="30" BorderThickness="1"
VerticalContentAlignment="Center"
Padding="10,0"
x:Name="TestTextBox"
ToolTip="{StaticResource MyArray}">
</TextBox>
<Button Grid.Column="1" Content="搜索" Margin="10,0" Height="30" Click="ButtonBase_OnClick"></Button>
<Popup Grid.Column="0"
AllowsTransparency="True"
PopupAnimation="Slide"
PlacementTarget="{Binding ElementName=TestTextBox}"
Placement="Bottom"
Width="{Binding ElementName=TestTextBox, Path=ActualWidth}"
IsOpen="{Binding ElementName=TestTextBox, Path=IsKeyboardFocused, Mode=OneWay}">
<ListBox ItemsSource="{Binding ElementName=TestTextBox, Path=ToolTip}"
SelectedItem="{Binding ElementName=TestTextBox, Path=Text, Mode=OneWayToSource}"></ListBox>
</Popup>
</Grid>

为了,让Popup控件能够跟随目标控件移动,需要增加一个工具类,具体如下所示:

 public class PopupHelper
{
/// <summary>
/// 附加属性:跟随放置控件而移动
/// </summary>
public static readonly DependencyProperty PopupPlacementTargetProperty = DependencyProperty.RegisterAttached("PopupPlacementTarget", typeof(DependencyObject), typeof(PopupHelper), new PropertyMetadata(null, OnPopupPlacementTargetChanged)); public static DependencyObject GetPopupPlacementTarget(DependencyObject obj)
{
return (DependencyObject)obj.GetValue(PopupPlacementTargetProperty);
}
public static void SetPopupPlacementTarget(DependencyObject obj, DependencyObject value)
{
obj.SetValue(PopupPlacementTargetProperty, value);
} private static void OnPopupPlacementTargetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is System.Windows.Controls.Primitives.Popup pop && e.NewValue is DependencyObject placementTarget)
{
var window = Window.GetWindow(placementTarget);
if (window != null)
{
window.LocationChanged += delegate
{
var mi = typeof(System.Windows.Controls.Primitives.Popup).GetMethod("UpdatePosition", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
mi?.Invoke(pop, null);
};
}
}
}
}

该附加属性的使用如下所示:

 <Popup local:PopupHelper.PopupPlacementTarget="{Binding ElementName=TestTextBox}"></Popup>

WPF中Popup控件的使用的更多相关文章

  1. WPF中Popup控件在Win7以及Win10等中的对齐点方式不一样的解决方案 - 简书

    原文:WPF中Popup控件在Win7以及Win10等中的对齐点方式不一样的解决方案 - 简书 最近项目中使用弹出控件Popup,发现弹出框的对齐方式在不同的系统中存在不同(Popup在win10上是 ...

  2. 关于WPF中Popup控件的小记

    在wpf开发中,常需要在鼠标位置处弹出一个“提示框”(在此就以“提示框”代替吧),通过“提示框”进行信息提示或者数据操作,如果仅仅是提示作用,使用ToolTip控件已经足够,但是有些是需要在弹出的框中 ...

  3. WPF中Ribbon控件的使用

    这篇博客将分享如何在WPF程序中使用Ribbon控件.Ribbon可以很大的提高软件的便捷性. 上面截图使Outlook 2010的界面,在Home标签页中,将所属的Menu都平铺的布局,非常容易的可 ...

  4. wpf中dropdownButton控件下拉居中。。。

    设置模版中popup控件的HorizontalOffset属性来控制居中. 还是对popup控件不熟,折腾了一会.

  5. WPF中查找控件的扩展类

    在wpf中查找控件要用到VisualTreeHelper类,但这个类并没有按照名字查找控件的方法,于是搜索网络,整理出下面这个类,感觉用起来很是方便. 贴出来,供大家参考. /// <summa ...

  6. WPF中Image控件的Source属性

    原文:WPF中Image控件的Source属性 imgBook 是一个Image控件,在后台代码中我想给它指定Source的属性.我先如下方式进行: Uri uri = new Uri(strImag ...

  7. WPF中PasswordBox控件的Password属性的数据绑定

    原文:WPF中PasswordBox控件的Password属性的数据绑定 英文原文:http://www.wpftutorial.net/PasswordBox.html 中文原文:http://bl ...

  8. 浅谈WPF中对控件的位图特效(WPF Bitmap Effects)

    原文:浅谈WPF中对控件的位图特效(WPF Bitmap Effects) -------------------------------------------------------------- ...

  9. 示例:WPF中Slider控件封装的缓冲播放进度条控件

    原文:示例:WPF中Slider控件封装的缓冲播放进度条控件 一.目的:模仿播放器播放进度条,支持缓冲任务功能 二.进度: 实现类似播放器中带缓存的播放样式(播放区域.缓冲区域.全部区域等样式) 实现 ...

随机推荐

  1. Mybatis-typeAliases的作用

    其他具体代码请访问->mybatis的快速入门 1.在mybatis核心配置文件SqlMapperConfig.xml中配置别名 <!-- 定义别名--> <typeAlias ...

  2. Java学习day14

    可变参数用作方法的形参,方法参数的个数就可变 格式:修饰符 返回值类型 方法名(数据类型...变量名){ } 方法内的形参只能有一个,这里的变量是一个数组 public static <T> ...

  3. 内存之旅——如何提升CMA利用率?

    ​(以下内容来自开发者分享,不代表 OpenHarmony 项目群工作委员会观点)​ 宋远征 李佳伟 OpenAtom OpenHarmony(以下简称"OpenHarmony") ...

  4. AngularJS性能优化心得,自己踩过的抗,及一些别人的经验(转哦)

    脏数据检查 != 轮询检查更新 谈起angular的脏检查机制(dirty-checking), 常见的误解就是认为: ng是定时轮询去检查model是否变更.其实,ng只有在指定事件触发后,才进入$ ...

  5. ThinkPHP3.2.3反序列化链子分析

    前言 目前官方已经不再维护ThinkPHP3.2.3,本文仅对ThinkPHP3.2.3反序列化链子进行复现,如有纰漏,还望指正. 环境介绍 MAMP pro PhpStorm Xdebug 利用条件 ...

  6. git 在 pull 或者合并分支的时候会遇到下图这个界面

      可以不管(直接进入 3, 4 步), 如果要输入解释的话就需要 按键盘字母 i 进入 insert 模式 修改最上面那行黄色合并信息,可以不修改 // 黄色内容为默认的合并信息; 按键盘左上角 & ...

  7. 【GPLT】 图着色问题(c++)

    题目如下: 这道题就是奇葩,多少有点低质量,这题不难,知识点就是邻接矩阵,但有以下奇葩点 1.颜色的编号是1-v 不是1-k,这点卡了我一会: 2.颜色涂色可以多于3,也可以少于3(这其实正常,但如果 ...

  8. Windows10 office 点击链接提示您的组策略阻止我们为您完成此操作。设置ChromeHTML也无效.

    问题: win10环境点击office 中的网络链接时  出现了如下报错(一般在卸载了系统预装的其他浏览器后出现问题) 解决方案: 1.设置默认浏览器(已经设置可跳过) 控制面板->设置小图标 ...

  9. XCTF练习题---MISC---Janos-the-Ripper

    XCTF练习题---MISC---János-the-Ripper flag:flag{ev3n::y0u::bru7us?!} 解题步骤: 1.观察题目,下载附件 2.发现是压缩包,进行解压,是一个 ...

  10. 单列集合(Collection-Set)

    (部分) Set类特点: "无序"(输入顺序和存储顺序不一样) HashSet 底层是HashMap 关于不能有重复元素/对象 遇到的问题: 解决办法:重新类的相关方法 选择名字和 ...