-1

I'm unable to bind the <ContentControl Content={Binding Type}/> to the local resources <DataTemplate> when binding my custom property on my POCO class called Type (I'm hoping it's not down to the bad choice of naming).

ReportItemModel

public class ReportItemModel
{
        public ReportItemModel(string name, ReportItemType itemType, Type type)
        {
            Name = name;
            ItemType = itemType;
            Type = type;
        }

        public string Name { get; }
        public ReportItemType ItemType  { get; }
        public Type Type { get; }
}

XAML

<ContentControl Content="{Binding Type}"  Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="3">

      <ContentControl.Resources>
            <DataTemplate DataType="{x:Type moduleTypes:ReportModule}">
                   <Label>Test</Label>
             </DataTemplate>
      </ContentControl.Resources>

</ContentControl>

The Type property is (as you've probably guessed) a System.Type and the output within my WPF application is always the output of ToString() which I'm guessing is down to a failed match of my <DataTemplate>. If I change the ContentControl to Content={Binding} and set the <DataTemplate> to accept the data type of the POCO class ReportItemModel it works as intended. The only difference I can see is that ReportItemModel has been instantiated where as the Type property has not.

Kitson88
  • 2,889
  • 5
  • 22
  • 37

1 Answers1

-1

The issue was down to the XAML DataType="{x:Type moduleTypes:ReportModule}" calling behind the scenes GetType() on a System.Type which would always return a System.RuntimeType as the object was never instantiated and always (at that point) a System.Type (a "Brain Fart" moment to say the least). I was able to get around the issue by using a ValueConverter on the binding <ContentControl Content={Binding Type} and returning the BaseClass name as a string. Using the BaseClass name, It was easy enough to use a couple ofDataTriggers which changed the ContentTemplate value to one of my matched custom DataTemplate's using a similar method shown here.

Kitson88
  • 2,889
  • 5
  • 22
  • 37