根据网上各种方法,优化了窗体变化放大缩小时的BUG,完美解决WPF中Popup控件跟随移动和始终位于最上层问题
1.重写popup控件
public class PopupEx : Popup
{
/// <summary>
/// 是否窗口随动,默认为随动(true)
/// </summary>
public bool IsPositionUpdate
{
get { return (bool)GetValue(IsPositionUpdateProperty); }
set { SetValue(IsPositionUpdateProperty, value); }
}
public static readonly DependencyProperty IsPositionUpdateProperty =
DependencyProperty.Register("IsPositionUpdate", typeof(bool), typeof(PopupEx), new PropertyMetadata(true, new PropertyChangedCallback(IsPositionUpdateChanged)));
private static void IsPositionUpdateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as PopupEx).pup_Loaded(d as PopupEx, null);
}
/// <summary>
/// 加载窗口随动事件