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:

C# saving a bitmap results in generic GDI+ error

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

System.Runtime.InteropServices.ExternalException (0x80004005): A generic error occurred in GDI+.
   at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)

Possible causes:

C# Scale bitmap

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

C# Multithreading

Execute code in the UI Thread

Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, () => {
                Mouse.OverrideCursor = Cursors.Wait;

Log4Net take the name of the actual class for the logger


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:
    [DllImport("user32.dll", EntryPoint="SetWindowLongA", SetLastError=true)]
    		private static extern long SetWindowLong (IntPtr hwnd, int nIndex, long dwNewLong);

    for example to

C# Show Console in WPF App

Use the following snippet to declare the Win32 function

        [DllImport("kernel32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool AllocConsole();

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.

cd C:\Windows\Microsoft.NET\Framework64\v2.0.50727
ldr64 setwow

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:

	Set WShell = WScript.CreateObject("WScript.Shell")

instead of

	Set WShell = CreateObject("WScript.Shell")

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:



Subscribe to RSS - Programming