WPF Popup
参考:
Popup 消息弹出框,参考:https://www.cnblogs.com/MingQiu/p/18005673
https://www.cnblogs.com/sntetwt/p/11345072.html
https://www.cnblogs.com/hsiang/p/17955617
https://docs.microsoft.com/zh-cn/dotnet/desktop/wpf/controls/how-to-specify-a-custom-popup-position?view=netframeworkdesktop-4.8
示例:
XAML代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | < Grid > < StackPanel > < RichTextBox x:Name="myRichTextBox" VerticalScrollBarVisibility="Auto" IsReadOnly="True"> < local:TalkFlowDocument > < Paragraph > < Button Click="myButtonLeft_Click"> < Button.Template > < ControlTemplate > < Image Source="/images/github.png" Width="64" Height="64" Stretch="Fill"/> </ ControlTemplate > </ Button.Template > </ Button > </ Paragraph > < Paragraph TextAlignment="Right"> < Button Click="myButtonRight_Click"> < Button.Template > < ControlTemplate > < Image Source="/images/github.png" Width="64" Height="64" Stretch="Fill"/> </ ControlTemplate > </ Button.Template > </ Button > </ Paragraph > </ local:TalkFlowDocument > </ RichTextBox > </ StackPanel > <!--放在页面最底部--> < Popup Name="myPopup" Height="300" Placement="Custom" Width="300" StaysOpen="False" IsOpen="False" PopupAnimation="Fade"> < Border Background="Red"> </ Border > </ Popup > </ Grid > |
C# 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | public WindowPopup() { InitializeComponent(); myPopup.CustomPopupPlacementCallback = new CustomPopupPlacementCallback(placePopup); } public CustomPopupPlacement[] placePopup(Size popupSize, Size targetSize, Point offset) { CustomPopupPlacement placementVertical = new CustomPopupPlacement( new Point(0, 0), PopupPrimaryAxis.Vertical); CustomPopupPlacement placementHorizontal = new CustomPopupPlacement( new Point(0, 0), PopupPrimaryAxis.Horizontal); CustomPopupPlacement[] ttplaces = new CustomPopupPlacement[] { placementVertical, placementHorizontal }; return ttplaces; } private void myButtonLeft_Click( object sender, RoutedEventArgs e) { Point mousePoint = Mouse.GetPosition( this ); myPopup.VerticalOffset = mousePoint.Y; myPopup.HorizontalOffset = mousePoint.X + 20; myPopup.IsOpen = false ; myPopup.IsOpen = true ; } private void myButtonRight_Click( object sender, RoutedEventArgs e) { Point mousePoint = Mouse.GetPosition( this ); myPopup.VerticalOffset = mousePoint.Y; myPopup.HorizontalOffset = mousePoint.X - 300; myPopup.IsOpen = false ; myPopup.IsOpen = true ; } } public class TalkFlowDocument : FlowDocument { protected override bool IsEnabledCore { get { return true ; } } } |
注:允许 RichTextBox 点击,事件点击对象为 Button
1 2 3 4 5 6 7 8 9 10 | public class TalkFlowDocument : FlowDocument { protected override bool IsEnabledCore { get { return true ; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | < ToggleButton x:Name="TogglePopupButton" Height="28" Width="40" HorizontalAlignment="Left"> < ToggleButton.Template > < ControlTemplate TargetType="ToggleButton"> < Grid Background="Transparent"> < Path Stretch="Fill" Width="21" Height="5" Fill="#666" Data="M213.333333 512a85.333333 85.333333 0 1 1-85.333333-85.333333 85.333333 85.333333 0 0 1 85.333333 85.333333z m298.666667-85.333333a85.333333 85.333333 0 1 0 85.333333 85.333333 85.333333 85.333333 0 0 0-85.333333-85.333333z m384 0a85.333333 85.333333 0 1 0 85.333333 85.333333 85.333333 85.333333 0 0 0-85.333333-85.333333z"/> < Popup Placement="Left" VerticalOffset="28" HorizontalOffset="40" IsOpen="{Binding IsChecked, ElementName=TogglePopupButton}" StaysOpen="False" PopupAnimation="Fade"> < Border BorderThickness="1"> < TextBlock Name="myPopupText" Text="这是一段弹出内容" Background="LightBlue" Foreground="Blue" Padding="30"></ TextBlock > </ Border > </ Popup > </ Grid > </ ControlTemplate > </ ToggleButton.Template > </ ToggleButton > |
注意:
StaysOpen="False" 点击其他地方则隐藏
分类:
WPF 控件
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?