2024年2月8日发(作者:)

<> 1.2 cs

public partial class TextBoxWithErrorPop : TextBox,INotifyPropertyChanged { public bool _ErrOpen = false; public bool ErrOpen { get { return _ErrOpen; } set { _ErrOpen = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ErrOpen")); } } public ScrollContentPresenter ParentSv; public Window ParentWin; public TextBoxWithErrorPop() { InitializeComponent(); } public event PropertyChangedEventHandler PropertyChanged; public void TextBox_TextChanged(object sender, TextChangedEventArgs e) { ErrOpen = false; } public void TextBox_Loaded(object sender, RoutedEventArgs e) { ParentSv = TryGetParent(this); ParentWin = TryGetParent(this); if (ParentSv != null) { Changed += (s, ea) => { if (ErrOpen) { ErrOpen = false; } }; } if (ParentWin != null) { onChanged += (s,ea) => { if (ErrOpen) { ErrOpen = false; } }; } } public T TryGetParent(DependencyObject element) where T : DependencyObject { if (element == null) return null; DependencyObject parent = ent(element); if (parent == null) return null; T parentTr = parent as T; if (parentTr != null) return parentTr; return TryGetParent(parent); } }1.3 PopupNonTopmost【Topmost = false】class PopupNonTopmost : Popup {

{ ///

/// Is Topmost dependency property /// public static readonly DependencyProperty IsTopmostProperty = er("IsTopmost", typeof(bool), typeof(PopupNonTopmost), new FrameworkPropertyMetadata(false, OnIsTopmostChanged)); private bool? _appliedTopMost; private bool _alreadyLoaded; private Window _parentWindow; /// /// Get/Set IsTopmost /// public bool IsTopmost { get { return (bool)GetValue(IsTopmostProperty); } set { SetValue(IsTopmostProperty, value); } } /// /// ctor /// public PopupNonTopmost() { Loaded += OnPopupLoaded; Unloaded += OnPopupUnloaded; } void OnPopupLoaded(object sender, RoutedEventArgs e) { if (_alreadyLoaded) return; _alreadyLoaded = true; if (Child != null) { dler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(OnChildPreviewMouseLeftButtonDown), true); } _parentWindow = dow(this); if (_parentWindow == null) return; _ted += OnParentWindowActivated; _vated += OnParentWindowDeactivated; } private void OnPopupUnloaded(object sender, RoutedEventArgs e) { if (_parentWindow == null) return; _ted -= OnParentWindowActivated; _vated -= OnParentWindowDeactivated; } void OnParentWindowActivated(object sender, EventArgs e) { SetTopmostState(true); } void OnParentWindowDeactivated(object sender, EventArgs e) {

{ if (IsTopmost == false) { SetTopmostState(IsTopmost); } } void OnChildPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { SetTopmostState(true); if (!_ve && IsTopmost == false) { _te(); } } private static void OnIsTopmostChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { var thisobj = (PopupNonTopmost)obj; mostState(ost); } protected override void OnOpened(EventArgs e) { //_appliedTopMost = null; SetTopmostState(IsTopmost); ed(e); } private void SetTopmostState(bool isTop) { // Don’t apply state if it’s the same as incoming state if (_ue && _appliedTopMost == isTop) { return; } if (Child == null) return; var hwndSource = (sual(Child)) as HwndSource; if (hwndSource == null) return; var hwnd = ; RECT rect; if (!GetWindowRect(hwnd, out rect)) return; if (isTop) { SetWindowPos(hwnd, HWND_TOPMOST, , , (int)Width, (int)Height, TOPMOST_FLAGS); } else { // Z-Order would only get refreshed/reflected if clicking the // the titlebar (as opposed to other parts of the external // window) unless I first set the popup to HWND_BOTTOM // then HWND_TOP before HWND_NOTOPMOST SetWindowPos(hwnd, HWND_BOTTOM, , , (int)Width, (int)Height, TOPMOST_FLAGS); SetWindowPos(hwnd, HWND_TOP, , , (int)Width, (int)Height, TOPMOST_FLAGS);

SetWindowPos(hwnd, HWND_TOP, , , (int)Width, (int)Height, TOPMOST_FLAGS); SetWindowPos(hwnd, HWND_NOTOPMOST, , , (int)Width, (int)Height, TOPMOST_FLAGS); } _appliedTopMost = isTop; } #region P/Invoke imports & definitions #pragma warning disable 1591 //Xml-doc #pragma warning disable 169 //Never used-warning // ReSharper disable InconsistentNaming // Imports etc. with their naming rules [StructLayout(tial)] public struct RECT { public int Left; public int Top; public int Right; public int Bottom; } [DllImport("")] [return: MarshalAs()] private static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect); [DllImport("")] private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); static readonly IntPtr HWND_TOPMOST = new IntPtr(-1); static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2); static readonly IntPtr HWND_TOP = new IntPtr(0); static readonly IntPtr HWND_BOTTOM = new IntPtr(1); private const UInt32 SWP_NOSIZE = 0x0001; const UInt32 SWP_NOMOVE = 0x0002; const UInt32 SWP_NOZORDER = 0x0004; const UInt32 SWP_NOREDRAW = 0x0008; const UInt32 SWP_NOACTIVATE = 0x0010; const UInt32 SWP_FRAMECHANGED = 0x0020; /* The frame changed: send WM_NCCALCSIZE */ const UInt32 SWP_SHOWWINDOW = 0x0040; const UInt32 SWP_HIDEWINDOW = 0x0080; const UInt32 SWP_NOCOPYBITS = 0x0100; const UInt32 SWP_NOOWNERZORDER = 0x0200; /* Don’t do owner Z ordering */ const UInt32 SWP_NOSENDCHANGING = 0x0400; /* Don’t send WM_WINDOWPOSCHANGING */ const UInt32 TOPMOST_FLAGS = SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOSIZE | SWP_NOMOVE | SWP_NOREDRAW | SWP_NOSENDCHANGING; // ReSharper restore InconsistentNaming #pragma warning restore 1591 #pragma warning restore 169 #endregion }