Search code examples
c#xamlwindows-store-appsuwp

Is there any way I can use value types in x:DataType?


Given this DataTemplate:

<DataTemplate x:DataType="Color">
    ...
</DataTemplate>

I get the following error:

The as operator must be used with a reference type or nullable type ('Color' is a non-nullable value type)

When you follow the error, it takes you to auto-generated code for that view which uses the as operator.

public void DataContextChangedHandler(global::Windows.UI.Xaml.FrameworkElement sender, global::Windows.UI.Xaml.DataContextChangedEventArgs args)
{
        global::Windows.UI.Color data = args.NewValue as global::Windows.UI.Color;
        if (args.NewValue != null && data == null)
        {
        throw new global::System.ArgumentException("Incorrect type passed into template. Based on the x:DataType global::Windows.UI.Color was expected.");
        }
        this.SetDataRoot(data);
        this.Update();
}

I know that {x:Bind} is new, but just in case, does anyone know how to configure it to allow value types, or at least use direct casting?


Solution

  • I have the same issue when binding the Windows Runtime Type like “Windows.UI.Color” in x:DateType.

    The current workaround I used is wrapping a .NET Reference Type.

    public class BindModel
    {
        public Windows.UI.Color Color { get; set; }
    }
    
    <DataTemplate x:Key="test" x:DataType="local:BindModel">
        <TextBlock>
            <TextBlock.Foreground>
                <SolidColorBrush Color="{x:Bind Color}"></SolidColorBrush>
            </TextBlock.Foreground>
        </TextBlock>
    </DataTemplate>