WPF:如何将对象数据绑定到元素并通过代码应用数据模板?
我创建了一个类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()
{
}
}
接下来,我在 XAML 中为 LeagueMatch
对象定义了一个数据模板资源:
<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);
I have created a class 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()
{
}
}
Next, I have defined a datatemplate resource for LeagueMatch
objects in 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>
In the XAML code-behind, I want to create a ContentPresenter
object and to set it's data binding to a previously initialized LeagueMatch
object and apply the defined data template. How is that supposed to be done? Thanks.
Solution
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在代码隐藏中:
实现
INotifyPropertyChanged
请务必通过分配给属性来仅更改属性,以便执行事件。绑定引擎正在侦听这些事件以刷新绑定。
In code-behind:
Implementing
INotifyPropertyChanged
Be sure to change your properties only by assigning to the property so the event gets executed. The binding engine is listening to those events to refresh the binding.
您需要绑定到 ContentPresenter 上的 Content 属性,而不是 DataContext,才能将模板应用到您的数据:
You need to bind to the Content property on the ContentPresenter, not DataContext, to apply the template to your data: