一、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. Kubernetes架构-图解

  2. Druid 1.1.24 在控制台打印"discard long time none received connection. , jdbcUrl : jdbc:mysql://...."错误日志

    Druid 1.1.24 在控制台打印"discard long time none received connection. , jdbcUrl : jdbc:mysql://...&qu ...

  3. pycharm的安装指导教程以及破解

    第一步:下载 下载网址:https://www.jetbrains.com/pycharm/download/#section=windows 根据自己的操作系统选择,然后点击download就开始下 ...

  4. Python学习笔记.md

    Python学习笔记 1.变量类型 x=5 int x="ss" string x='a' string x=True bool #查看变量类型 type(x) 2.字符串常用操作 ...

  5. Blazor 使用拖放(drag and drop)上传文件

    在很多上传文件的应用实例中, 都可以看到[拖放文件到此上传]这种骚功能 ,今天我们就来试试Blazor能不能完成这个想法. 简述HTML5拖放 拖放是HTML5标准的一部分,任何元素都能够拖放,也能够 ...

  6. Codeforces Round #742 (Div. 2) B. MEXor Mixup

    题目链接 Problem - B - Codeforces 题意: 给出MEX 和 XOR(分别表示1. 本串数不存在的最小非负数  2. 本串数所有数异或后的结果) 求出这串数最少有几个数, 1 ≤ ...

  7. nodejs mysql pool 只能插入10条记录或者较少记录

    BEGIN; 解决方案:从连接池获取到的Connection,执行完操作后,必须及时关闭! 即:connection.end(); 使用后发现console有打印出警告信息,大致意思为 end() 方 ...

  8. Vue_基础功能循环、计算、绑定、事件处理、组件

    1 <!DOCTYPE html> 2 <html lang="en" xmlns:v-bind="http://www.w3.org/1999/xht ...

  9. 动态规划 Dynamic Programming 学习笔记

    文章以 CC-BY-SA 方式共享,此说明高于本站内其他说明. 本文尚未完工,但内容足够丰富,故提前发布. 内容包含大量 \(\LaTeX\) 公式,渲染可能需要一些时间,请耐心等待渲染(约 5s). ...

  10. JavaScript 数据结构与算法1(数组与栈)

    学习数据结构的 git 代码地址: https://gitee.com/zhangning187/js-data-structure-study 1.数组 几乎所有的语言都原生支持数组类型,因为数组是 ...