日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

WPF 记一个Popup踩坑记录

發布時間:2023/12/4 asp.net 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF 记一个Popup踩坑记录 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

看名字就知道,它是一個彈出控件,顧名思義,我們可以用它來實現類似Combobox那種,點擊后彈出下面選項列表的操作。

記錄:

需求:有一個文本框 ,鼠標點擊后,彈出一個Popup。

我編寫了以下xaml

<Grid><TextBox PreviewMouseDown="text_PreviewMouseDown" x:Name="text" Text="666" BorderBrush="Red" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top"Width="100" FontSize="30" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/><Popup Placement="Bottom" PlacementTarget="{Binding ElementName=text}"IsOpen="{Binding IsOpen1}"AllowsTransparency="True" StaysOpen="False" Width="200" Height="200"><Border Margin="10" Background="Green"><TextBox Text="彈出內容1" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></Popup> </Grid>

這時,奇怪的現象出現了,我鼠標點擊textbox后,一松開,popup就消失了。。。

經過一番研究,發現。

問題出在textbox的點擊事件上,在PreviewMouseDown事件執行完畢之后,焦點會移到textbox上,這里popup就失去焦點了。。

編寫以下MainWindow.xaml:

<Window x:Class="wpfcore.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:wpfcore"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions><Grid><TextBox PreviewMouseUp="text_PreviewMouseDown" x:Name="text" Text="666" BorderBrush="Red" BorderThickness="1" HorizontalAlignment="Left" VerticalAlignment="Top"Width="100" FontSize="30" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/><Popup Placement="Bottom" PlacementTarget="{Binding ElementName=text}"IsOpen="{Binding IsOpen1}"AllowsTransparency="True" StaysOpen="False" Width="200" Height="200"><Border Margin="10" Background="Green"><TextBox Text="彈出內容1" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></Popup></Grid><Grid Grid.Column="1"><TextBlock MouseUp="textBlock_PreviewMouseDown" x:Name="textblock" Text="666" Background="LightBlue" HorizontalAlignment="Left" VerticalAlignment="Top"Width="100" FontSize="30" TextAlignment="Center"/><Popup Placement="Bottom" PlacementTarget="{Binding ElementName=textblock}"IsOpen="{Binding IsOpen2}"AllowsTransparency="True" StaysOpen="False" Width="200" Height="200"><Border Margin="10" Background="Green"><TextBox Text="彈出內容2" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></Popup></Grid></Grid> </Window>

MainWindow.cs代碼如下:

using System.Windows; using System.Windows.Input;namespace wpfcore {public partial class MainWindow : Window{public bool IsOpen1{get { return (bool)GetValue(IsOpen1Property); }set { SetValue(IsOpen1Property, value); }}public static readonly DependencyProperty IsOpen1Property =DependencyProperty.Register("IsOpen1", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));public bool IsOpen2{get { return (bool)GetValue(IsOpen2Property); }set { SetValue(IsOpen2Property, value); }}public static readonly DependencyProperty IsOpen2Property =DependencyProperty.Register("IsOpen2", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));public MainWindow(){InitializeComponent();DataContext = this;}private void text_PreviewMouseDown(object sender, MouseButtonEventArgs e){IsOpen1 = true;}private void textBlock_PreviewMouseDown(object sender, MouseButtonEventArgs e){IsOpen2 = true;}} }

通過對比可以發現,使用一個TextBlock的效果比textbox好多了。。

以下是對比效果:

記錄下來,備查。

總結

以上是生活随笔為你收集整理的WPF 记一个Popup踩坑记录的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。