Popup中ListBox的SelectChange事件關閉彈出窗體後主窗體點擊無效BUG

来源:https://www.cnblogs.com/wandia/archive/2020/04/15/12703620.html
-Advertisement-
Play Games

WPF的BUG!彈出框的 自定義控制項里有Popup, Popup裡面放一個ListBox 在ListBox中的SelectionChange事件觸發關閉彈出框後,主窗體存在一定概率卡死(但點擊標題又能用的BUG) 步驟一: 新建個自定義WPF控制項UserControl Xaml代碼: <UserCo ...


WPF的BUG!
彈出框的 自定義控制項里有Popup, Popup裡面放一個ListBox

 在ListBox中的SelectionChange事件觸發關閉彈出框後,主窗體存在一定概率卡死(但點擊標題又能用的BUG)

 

步驟一: 新建個自定義WPF控制項UserControl

Xaml代碼:

<UserControl x:Class="WpfApplication1.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WpfApplication1"
             mc:Ignorable="d" 
                 d:DesignHeight="30" d:DesignWidth="200">
    <Grid>
        <Grid x:Name="PART_Container">
            <DockPanel Margin="0,0,1,0">
                <ToggleButton x:Name="PART_ToggBtn" DockPanel.Dock="Right" BorderThickness="1" BorderBrush="#959595" Margin="-1,0,0,0" 
                          IsChecked="{Binding Path=IsDropDownOpen,Mode=TwoWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:UserControl1}}">
                    >
                </ToggleButton>
                <TextBox  x:Name="txtAutoComplete" />
            </DockPanel>
        </Grid>
        <Popup x:Name="PART_Popup" Opacity="0" Width="{Binding ElementName=PART_Container,Path=ActualWidth}"
               IsOpen="{Binding Path=IsDropDownOpen,Mode=OneWay, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:UserControl1}}"
               PopupAnimation="Slide" Placement="Bottom" StaysOpen="False" AllowsTransparency="True"
               PlacementTarget="{Binding ElementName=txtAutoComplete}" VerticalOffset="0" MinHeight="50" MaxHeight="300">
            <ListBox x:Name="listboxSuggestion" BorderBrush="Transparent" BorderThickness="0">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Item1}"/>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Popup>
    </Grid>
</UserControl>

邏輯代碼:

    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
            List<Tuple<string, string, string>> tupes = new List<Tuple<string, string, string>>();
            Enumerable.Range(1, 10).Select(p => p.ToString().PadLeft(3, '0')).ToList().ForEach(p => tupes.Add(new Tuple<string, string, string>(p, p, p)));
            listboxSuggestion.ItemsSource = tupes;
            listboxSuggestion.SelectionChanged += (o1, e1) =>
            {
                RoutedEventArgs args = new RoutedEventArgs(EnterDownEvent, o1); //選中項改變觸發
                this.RaiseEvent(args);
            };
        }

        #region 回車觸發事件
        //聲明和註冊路由事件
        public static readonly RoutedEvent EnterDownEvent =
            EventManager.RegisterRoutedEvent(
                "EnterDown",
                RoutingStrategy.Bubble,
                typeof(EventHandler<RoutedEventArgs>),
                typeof(UserControl1));
        //CLR事件包裝
        public event RoutedEventHandler EnterDown
        {
            add { this.AddHandler(EnterDownEvent, value); }
            remove { this.RemoveHandler(EnterDownEvent, value); }
        }
        #endregion


        #region 是否打開下拉框
        public bool IsDropDownOpen
        {
            get { return (bool)GetValue(IsDropDownOpenProperty); }
            set
            {

                SetValue(IsDropDownOpenProperty, value);

            }
        }
        public static readonly DependencyProperty IsDropDownOpenProperty =
            DependencyProperty.Register("IsDropDownOpen", typeof(bool), typeof(UserControl1), new PropertyMetadata(false));
        #endregion
    }

步驟二: 新建個Window窗體DialogWin

xaml代碼

<Window x:Class="WpfApplication1.DialogWin"
        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:WpfApplication1"
        mc:Ignorable="d"
        Title="DialogWin" Height="88.846" Width="210">
    <Grid>
        <local:UserControl1 Width="120" Height="22" x:Name="mySelect"  />
    </Grid>
</Window>

cs代碼

   public partial class DialogWin : Window
    {
        public DialogWin()
        {
            InitializeComponent();
            mySelect.EnterDown += (o1, e1) =>
            {
                this.Close();
            };
        }
    }

  步驟三,在主窗體彈出DialogWin

xaml代碼

<Window x:Class="WpfApplication1.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:WpfApplication1"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="44,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <TextBox  HorizontalAlignment="Left" Height="23" Margin="198,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <TextBox  HorizontalAlignment="Left" Height="23" Margin="350,32,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="44,82,0,0"/>
        <Button x:Name="button1" Content="彈出對話框" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="156,82,0,0" Click="button1_Click"/>

    </Grid>
</Window>

  cs代碼

/// <summary>
    /// MainWindow.xaml 的交互邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var s = new DialogWin();
            s.Owner = this;
            s.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            s.ShowDialog();
        }
    }

運行程式....

解決方案: 開啟線程延遲關閉彈出體【最無語做法】

代碼下載


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • DevExpress是比較流行的控制項庫,使用者較多,不知道大家有沒有遇到MessageBox的確定、取消按鈕上的文字是英文的情況。 這是因為DevExpress壓根沒有中文語言包,至少我這邊看到的情況是這樣的。 不多說,直接上代碼。 1 using DevExpress.Xpf.Core; 2 us ...
  • 想持久運營一款web或移動端的產品,對內容進行必要的把關必不可少。這裡分享一個基於DFA演算法的高性能的敏感詞,臟詞的檢測過濾演算法類(c#). ...
  • 如果大家讀過dapper源碼,你會發現這內部有很多方法都用到了yield關鍵詞,那yield到底是用來幹嘛的,能不能拿掉,拿掉與不拿掉有多大的差別,首先上一段dapper中精簡後的Query方法,先讓大家眼見為實。 一:yield探究 1. 骨架代碼猜想 骨架代碼其實很簡單,方法的返回值是IEnum ...
  • 最近有個 pad 上的 APP 開發,聽到 APP 這個詞,然後就把它安排給我做了,可是那個 pad 裝的是Windows系統,我是 Android 開發啊。 無奈的我只能搞唄。以下不是專業的教程,只是自己學習的記錄。不適合系統學習,適合快速上手。(以下內容也是百度的結果,如有錯誤,敬請指正) 一、 ...
  • 【問題】 在項目運行生成時報錯,CS0006 C# 未能找到元數據文件 ···.dll 【我的問題原因】 B引用的C類庫 ,B與C Framework 版本不同,一般情況下 Framework 版本高的引用版本低的不會出現問題,反之可能會報以上錯誤。 【解決問題】 此處 A 引用 B,B 引用了 C ...
  • 1 安裝VSCODE REMOTE SSH插件 2 打開插件配置 shift+ctlr+P , 輸入remote-SSH:setting 勾選 Always reveal the SSH login terminal. 3 插件安裝完成之後,左邊菜單會多一個“遠程資源管理器“ 4 創建一個遠程 配置 ...
  • 我們在使用git源代碼管理的時候,不可避免會遇到換倉或者本地倉的記錄推送到一個新建的遠程倉中,這時候是無法直接推送的,需要解決歷史數據合併的問題。 常見的錯誤:fatal: refusing to merge unrelated histories 解決方案一:如果你還沒有進行將線上代碼拉到本地,第 ...
  • 1、ThreadPool與Task? 線程池的優點:① 降低資源消耗。通過重覆利用已創建的線程降低線程創建和銷毀造成的消耗。 ② 提高響應速度。當任務到達時,任務可以不需要等到線程創建就能立即執行。 ③ 提高線程的可管理性。線程是稀缺資源,如果無限制的創建,不僅會消耗系統資源,還會降低系統的穩定性, ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...