SRM
SRM

Reputation: 1377

How To Resolve Custom Property Name In XAML For a Winforms UserControl Hosted In WPF?

I've got a usercontrol that I wrote for an XNA based tile editor. This is a winforms based control that I am now trying to host in a WPF application.

I am using a WindowsFormsHost control to embed the winforms user control. On the winforms usercontrol I have a custom property called XnaBackground that is of type Microsoft.Xna.Framework.Color. I can see the property fine in the XAML in IntelliSense but when I try to set it I get a message in my XAML window that says 'Cannot convert "Microsoft.Xna.Framework.Blue"'.

I've tried to use a custom IValueConverter but since the property is not a dependency property nor is the control a dependency object the binding doesn't work.

Here is the property declaration on the winforms control:

    [Category("Appearance"), DescriptionAttribute("Gets/Sets a value indicating the background color to use.")]
    [Bindable(true)]
    public Color XnaBackground { get; set; }

And here is the XAML from my WPF application:

<Window x:Class="TileEditorWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TileEditorWPF"
    xmlns:winforms="clr-namespace:TileDisplay;assembly=TileDisplay"
    Title="MainWindow" Height="600" Width="800" Loaded="WindowLoaded">

<Window.Resources>
    <local:XnaColorConverter x:Key="colorConverter" />
</Window.Resources>
<DockPanel LastChildFill="true">
    <WindowsFormsHost Name="windowsFormsHost1" DockPanel.Dock="Top" 
                                      Background="Transparent"         
                                      HorizontalAlignment="Stretch"             
                                      VerticalAlignment="Stretch" >
        <winforms:TileDisplayControl XnaBackground="Blue" x:Name="tileDisplay1" OnDraw="TileDisplayControl_OnDraw" OnInitialize="TileDisplayControl_OnInitialize" />            
    </WindowsFormsHost>
</DockPanel>

I'm not a WPF expert so please forgive me if the answer is obvious. I've been banging my head for the last 4 hours and google searches have led me nowhere. It's such a niche problem that it's difficult to find any kind of trending with this particular subject.

Upvotes: 0

Views: 570

Answers (1)

Colin Smith
Colin Smith

Reputation: 12540

You need to implement a TypeConverter which knows how to convert the "Blue" string value you set in the XnaBackground property into a Microsoft.Xna.Framework.Color type.

Because you didn't create the Microsoft.Xna.Framework.Color type/don't have access its source there is no way for you to put the [TypeConverter(typeof(ToXnaColorConverter)] on the type to point to your converter.

However you can put that attribute on the XnaBackground property instead. Thus, when a "string" is set on your XnaBackground property, it goes through your TypeConverter .... which should interpret the string and provide a suitable Xna.Framework.Color value.

[Category("Appearance"), DescriptionAttribute("Gets/Sets a value indicating the background color to use.")]
[Bindable(true)]
[TypeConverter(typeof(ToXnaColorConverter)] 
public Color XnaBackground { get; set; }

Upvotes: 2

Related Questions