I have this grid, containing a template:
<Grid x:Name="Bar" Width="400">
<Grid.Resources>
<DataTemplate x:Name="ScoreText">
<Grid Grid.Column="{Binding Col}">
<Rectangle Fill="Red"/>
<TextBlock Text="{Binding Value}" />
</Grid>
</DataTemplate>
</Grid.Resources>
</Grid
I want to insert an instance of the template into the containing grid, Bar. Here’s what I’m doing:
FrameworkElement item = (FrameworkElement) ScoreText.LoadContent();
item.DataContext = new { Col = 0, Value = 100 };
Bar.Children.Add(item);
This works in the XAML designer in visual studio, but fails to bind correctly on the phone.
What do I need to do to make this work?
Anonymous types don’t work, since only properties can be bound to. Adding:
And changing
to
makes it work