Set the focus on a TextBox in WPF XAML

You can use the FocusManager.FocusedElement attached property for this purpose. Here’s a piece of code that set the focus to TxtB by default. <StackPanel Orientation=”Vertical” FocusManager.FocusedElement=”{Binding ElementName=TxtB}”> <TextBox x:Name=”TxtA” Text=”A” /> <TextBox x:Name=”TxtB” Text=”B” /> </StackPanel> You can also use TxtB.Focus() in your code-behind if you don’t want to do this in XAML.

Workaround for WPF Freezable bug?

To workaround this .net bug, change all of the Solid Color Brushes in your code to be freezeable. For example <SolidColorBrush x:Key=”WindowBackground” Color=”Black” /> should be changed to: <SolidColorBrush po:Freeze=”True” x:Key=”WindowBackground” Color=”Black” />. For more detailed instructions see here: How can WPF objects deriving from Freezable be frozen in XAML?.

How do you do relative positioning in WPF?

Instead of putting (as in your example) a button directly on the canvas, you could put a stackpanel on the canvas, horizontally aligned, and put the two buttons in there. Like so: <Canvas> <StackPanel Canvas.Left=”100″ Canvas.Top=”100″ Orientation=”Horizontal”> <Button>Button 1</Button><Button>Button 2</Button> </StackPanel> </Canvas> I think that it’s quite flexible when you use more than 1 layout … Read more

SQLDependency_OnChange-Event fires only one single Time

I was running into this issue as well. You need to create a new SqlDependency entity (after unsubscribing the existing one from the OnChange event) and then run a new ExecuteReader command. I got the idea from this post: http://www.codeproject.com/Articles/12335/Using-SqlDependency-for-data-change-events This usually makes sense, as once you have been notified of a change you will … Read more

How to implement INotifyDataErrorInfo in WPF 4.5? [closed]

Beside the very detailed description on MSDN about the Silverlight version of INotifyDataErrorInfo There are already some blog posts with samples how to use/implement it in WPF 4.5: WPF 4.5 – Asynchronous validation (original link no longer working) Wayback Link WPF 4.5 – Part 1 : Asynchronous data validation (the author has a nice series … Read more

How to get MessageBox.Show() to pop up in the middle of my WPF application?

Here’s a version of the MessageBoxEx helper class posted in the other thread that uses WPF style message boxes (Note that you still need to reference System.Drawing): using System; using System.Text; using System.Runtime.InteropServices; using System.Windows; using System.Windows.Interop; using System.Drawing; public class MessageBoxEx { private static IntPtr _owner; private static HookProc _hookProc; private static IntPtr _hHook; … Read more