Silverlight UserControl Binding to Object Properties


On your main user control create a member variable for your object. Your object will need to implement the INotifyPropertyChanged interface. In your constructor, set the data context to the member variable. Your child user control will need to provide a dependency property and implement the INotifyPropertyChanged interface. Place your child user control on to the main user control, and set its binding in XAML to the name of the property, and set its mode to two way: Text="{Binding MyDataProperty, Mode="TwoWay}".

Update WPF UI During Method Execution


Samuel Jack of Seaturtle Software Limited has produced an interesting method that allows you to force the WPF UI to update during a method call. Here is the code:

void AllowUIToUpdate() {
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Render, new DispatcherOperationCallback(delegate(object parameter)
{
frame.Continue = false;
return null;
}), null);
Dispatcher.PushFrame(frame);
}

This code help me to fix an issue that I had with my WPF program. The original article is called Forcing Update of UI before my function exits.

Capturing WPF UserControl Key Events When Hosted In A WinForms ElementHost Control


I have had a very difficult problem that took nearly all day to solve. The problem was that I had a Windows Forms application with an ElementHost control for hosting WPF UserControls. I could quite easily manipulate the user controls with the mouse, because they are specifically designed to be used with the mouse. However, I became unstuck when I tried to implement abandoning the user controls via the F4 key press. No matter what I tried, I could not get any KeyPress, KeyDown, PreviewKeyPress, or PreviewKeyDown events to work. I tried various methods suggested by Microsoft and various forums. But to my dismay, none of these worked. Fortunately, after much banging my head against the wall I found a solution that worked.

The solution to the problem described above is to:

  1. Set the Focusable property of the WPF UserControl to true;
  2. In the WPF UserControl’s Loaded event, set the focus to the WPF UserControl;
  3. Finally, capture the WPF UserControl’s PreviewKeyDown event.

The solution works really well, and appears to fire consistently regardless of what I do on the WPF UserControl. If you face the same issue, then I hope this works for you.

Making WPF Images Display in High Quality


By default WPF images are display in a low quality resolution.  For images to be displayed in high quality, it is best to add the following code to your constructor after InititalizeComponent().

' Force images to display high quality.
RenderOptions.SetBitmapScalingMode(Me, BitmapScalingMode.HighQuality)

You can read amore indepth article on Stefan Olson’s blog at: http://www.olsonsoft.com/blogs/stefanolson/post/Workaround-for-low-quality-bitmap-resizing-in-WPF-4.aspx.

Windows Forms Element Host Fails To Display WPF User Control


Today I had the situation whereby my Windows Form would not display a WPF User Control within its Element Host Control. The source of the problem was that my WinForms project was set to target the .NET Framework 4 Client Profile. The solution was to change the setting under the compile options for the project to .NET Framework 4. Everything now works fine, although you may need to delete forms and user controls and recreate them against the new target framework.