Я создал класс LeagueMatch
.
public class LeagueMatch
{
public string Home
{
get { return home; }
set { home = value; }
}
public string Visitor
{
get { return visitor; }
set { visitor = value; }
}
private string home;
private string visitor;
public LeagueMatch()
{
}
}
Далее я определил ресурс таблицы данных для LeagueMatch
объектов в XAML:
<DataTemplate x:Key="MatchTemplate" DataType="{x:Type entities:LeagueMatch}">
<Grid Name="MatchGrid" Width="140" Height="50" Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Text="{Binding Home}" />
<TextBlock Grid.Row="1" Text="- vs -" />
<TextBlock Grid.Row="2" Text="{Binding Visitor}" />
</Grid>
</DataTemplate>
В коде XAML я хочу создать объект ContentPresenter
и установить привязку данных к ранее инициализированному объекту LeagueMatch
и применить определенный шаблон данных.Как это должно быть сделано?Благодарю.
Решение
LeagueMatch match = new LeagueMatch("A team", "B team");
ContentPresenter contentPresenter = new ContentPresenter();
contentPresenter.ContentTemplate = this.FindResource("MatchTemplate") as DataTemplate;
Binding binding = new Binding();
binding.Source = match;
contentPresenter.SetBinding(ContentPresenter.ContentProperty, binding);
matchSlot.Children.Clear();
matchSlot.Children.Add(contentPresenter);