WPF 记一个Popup踩坑记录
生活随笔
收集整理的這篇文章主要介紹了
WPF 记一个Popup踩坑记录
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
看名字就知道,它是一個彈出控件,顧名思義,我們可以用它來實現(xiàn)類似Combobox那種,點擊后彈出下面選項列表的操作。
記錄:
需求:有一個文本框 ,鼠標(biāo)點擊后,彈出一個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="彈出內(nèi)容1" FontSize="30" HorizontalAlignment="Center" VerticalAlignment="Center"/></Border></Popup> </Grid>這時,奇怪的現(xiàn)象出現(xiàn)了,我鼠標(biāo)點擊textbox后,一松開,popup就消失了。。。
經(jīng)過一番研究,發(fā)現(xiàn)。
問題出在textbox的點擊事件上,在PreviewMouseDown事件執(zhí)行完畢之后,焦點會移到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="彈出內(nèi)容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="彈出內(nèi)容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;}} }通過對比可以發(fā)現(xiàn),使用一個TextBlock的效果比textbox好多了。。
以下是對比效果:
記錄下來,備查。
總結(jié)
以上是生活随笔為你收集整理的WPF 记一个Popup踩坑记录的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: NET问答: 到底是返回 null 好,
- 下一篇: 删除未使用的引用 | Visual St