C# WPF Databinding

Databinding in General

If it doesn’t work check:

  • You can just bind to a property. Check that you are not binding on a field

Databinding for ContextMenus in General

ContextMenus in WPF are somehow special. They exist outside the visual tree of the parent control and therefore they are unable to find for example the root element by name. In the following example the binding doesn't work:

But after you use the following code in the Initialization in the Code behind file it does work:

Databinding Elements in a ComboBox

Define the class you want to show in the ComboBox:

Create a collection of the objects that you want to present in the ComboBox

Define the view where we want to show the the ComboBox

Set the correct DataContext in the views code behind:

Commands: Reevaluate CanExecute

If you change something so that a command would be enabled it is possible that WPF doesn’t reevaluate the state. It is possible however to force the system to reevalute:

Binding RadioButtons to an Enumeration

Source: See MVVM – Binding Multiple Radio Buttons To a single Enum Property in WPF or WPF radio buttons and enumeration values

Remember the following pitfall: If you enum is inside another class you need to use the + operator like

Two-way binding pitfall

If using two-way binding you need to specify a path or xpath. Therefore

will not work but

will.

NotifyCollecitonChangedAction pitfall

When listening for the NotifyCollectionChanged action of a collection one might think to catch every (regular) case when implementing the Add and Remove action. But the action Reset happens also quite frequently for example when calling Clear on a list

Call a method everytime a DependencyProperty changes

You can always listen to the general handler but sometimes the code gets more readable if a dedicated method is called for some property changes. This can be done with the following code:

Here is an example how to call it:

Read-only Dependency property

It is not enough to make the setter of a dependency property private and implementing it like regular read-only properties. The problem is that the value could be overwritten for example by animations. Here is the correct implementation:

C# saving a bitmap results in generic GDI+ error

If you save a bitmap the following exception can pop up:

Possible causes:

  • You tried to save the file with an invalid filename or in a directory that doesn’t exist
  • You constructed a bitmap from a memorystream that has been closed (the bitmap takes ownership of the MemoryStream and you are not allowed to close it yourself!)
  • You opened a bitmap from a file (-> which created a filestream) and you try to save another bitmap to the same file (-> doesn’t work because the file is blocked)

C# Scale bitmap

The following code scales a bitmap from the filesystem to a defined width:

C# Multithreading

Execute code in the UI Thread

PInvokeStackImbalance Excpetion

If you get a PInvokeStackImbalance exception it could be:

  • One parameter has a wrong type (p. x. int changed to long)
  • The CallingConvention of the DllImport. Change it in the declaration:

    for example to

C# Show Console in WPF App

Use the following snippet to declare the Win32 function

and this one to show the console

Run .NET Applications in 32-Bit Mode under 64-Bit Windows

Sometimes it is necessary to run .NET applications in 32-Bit mode. One example is the usage of Adobe Reader Interop Libraries which will crash if they run in 64-Bit mode.

This will force all .NET applications to be run in 32-Bit mode.

MSI Install Errror with CustomAction to execute a VBS-Script

The following error: "There is a problem with this Windows Installer package. A script required for this install to complete could not be run. Contact your support personnel or package vendor"

Usually results from using the line:

instead of

this is caused because inside a MSI vbs-Scripts are executed with a different object model than when running standalone.

 

 

Show PDF-Documents in a C# WPF Application

The easiest way to do this is to display the Adobe Reader Browser Plugin. This can be done with a UserControl which can be hosted in a WindowsFormsHost control in a WPF Form. You require a reference to the dlls AxInterop.AcroPDFLib.dll and Interop.AcroPDFLib.dll which you can find in the Reader SDK on the Adobe Developer Homepage. The code for a UserControl is shown below:

PDFControl.Designer.cs:

 

PDFControl.cs

In the WPF Form you can host it like:

And initialize it like:

To show a document: