WPF PreviewKeyDown Fired Multiple Times

I want to implement such feature: In a barcode text box, when a "=" key (or "+" key) is pressed, the "=" (or "+") character will not append to barcode textbox, and program will use textbox text as barcode to search product item.

 

To achieve this effect, we need use handle PreviewKeyDown event. Listening for KeyDown event is not working here, because when KeyDown event is fired, the new character is already appended to the textbox. (PreviewKeyDown is a tunneling event and KeyDown is a bubbling event)

But when I pressed "=" key, the PreviewKeyDown event is triggered multiple times.

 

By debugging I found only one event's IsDown property is true, while other triggered events' IsDown property is false and their IsUp property is true. To fix this solution we can check whether e.IsDown is true.

C# WPF Generate and Display QR Code

Download QrCode.Net

Download and install QrCode.Net from NuGet, there are two ways to install it.

Right click your project, select Manage NuGet Packages, in newly opened NuGet Package Manager tab, choose Browse tab and type QrCode.Net in Search text box, and install the QrCode.Net package by clicking the download icon at right side.

Or

Run following command in Package Manager Console

 

Create Container Window

Create a window with name MainWindow and add an Image control named QrCodeImage into it.

MainWindow.xaml

 

Display QR Code in Window

In MainWindow.xaml.cs MainWindow constructor function, add following code (should be located after InitializeComponent() method)

 

Running the project you will see the QR Code is displayed in the MainWindow.