»
S
I
D
E
B
A
R
«
Use RelativeSource with WPF bindings
May 3rd, 2010 by admin

There are many uses of RelativeSource with WPF bindings, here are the main ones:

To bind to another property on the object

{Binding Path=PathToProperty, RelativeSource={RelativeSource Self}}

To get a property on an ancestor

{Binding Path=PathToProperty, RelativeSource={RelativeSource AncestorType={x:Type typeOfAncestor}}}

To get a property on the templated parent

{Binding Path=PathToProperty, RelativeSource={RelativeSource TemplatedParent}}
Setting MenuItem.Icon in WPF
Apr 19th, 2010 by admin

If you have an images folder with png image in it and you would like to set a MenuItem’s icon to that png. Here is how you can write in procedural code:

menuItem.Icon = new BitmapImage(new Uri("images/sample.png", UriKind.Relative));
Blending WPF with XNA
Apr 19th, 2010 by admin

There are several ways to blend XNA and WPF within the same application. XNA integration is high on Microsoft’s list of things to add to WPF so they’re looking in to this for future versions. Here is more information you can find.

Handling Numeric Data Entry in WPF
Apr 19th, 2010 by admin

To get numeric data from the user without relying on a third-party control in WPF is easy, here is how you can do it:

protected override void OnPreviewTextInput(System.Windows.Input.TextCompositionEventArgs e)
    {
        e.Handled = !AreAllValidNumericChars(e.Text);
        base.OnPreviewTextInput(e);
    }

    private bool AreAllValidNumericChars(string str)
    {
        foreach(char c in str)
        {
            if(!Char.IsNumber(c)) return false;
        }

        return true;
    }