WPF:如何将对象数据绑定到元素并通过代码应用数据模板?

发布于 2024-10-08 11:34:24 字数 1626 浏览 8 评论 0原文

我创建了一个类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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴骨ら 2024-10-15 11:34:24
<ContentPresenter ContentTemplate="{StaticResource yourTemplate}" 
                   DataContext="{Binding Match}"/>

在代码隐藏中:

ContentPresenter c = new ContentPresenter();
c.ContentTemplate = FindResource("yourTemplate");
Binding b = new Binding();
b.Path = "Match"; // if the object is a property on someObject, otherwise leave it out.
b.Source = someObject;

c.SetBinding(ContentPresenter.ContentProperty, b);

实现 INotifyPropertyChanged

public class Sample : INotifyPropertyChanged
{
  private string sampleField = "";

  public string SampleProperty
  {
    get { return sampleField; }
    set
    {
       sampleField = value;
       RaisePropertyChanged("SampleProperty");
    }
  }

  #region INotifyPropertyChanged

  public event PropertyChangedEventHandler PropertyChanged;

  private void RaisePropertyChanged(string property)
  {
     var handler = PropertyChanged;
     if(handler != null)
        handler(this, new PropertyChangedEventArgs(property));
  }
}

请务必通过分配给属性来更改属性,以便执行事件。绑定引擎正在侦听这些事件以刷新绑定。

<ContentPresenter ContentTemplate="{StaticResource yourTemplate}" 
                   DataContext="{Binding Match}"/>

In code-behind:

ContentPresenter c = new ContentPresenter();
c.ContentTemplate = FindResource("yourTemplate");
Binding b = new Binding();
b.Path = "Match"; // if the object is a property on someObject, otherwise leave it out.
b.Source = someObject;

c.SetBinding(ContentPresenter.ContentProperty, b);

Implementing INotifyPropertyChanged

public class Sample : INotifyPropertyChanged
{
  private string sampleField = "";

  public string SampleProperty
  {
    get { return sampleField; }
    set
    {
       sampleField = value;
       RaisePropertyChanged("SampleProperty");
    }
  }

  #region INotifyPropertyChanged

  public event PropertyChangedEventHandler PropertyChanged;

  private void RaisePropertyChanged(string property)
  {
     var handler = PropertyChanged;
     if(handler != null)
        handler(this, new PropertyChangedEventArgs(property));
  }
}

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.

知足的幸福 2024-10-15 11:34:24

您需要绑定到 ContentPresenter 上的 Content 属性,而不是 DataContext,才能将模板应用到您的数据:

contentPresenter.SetBinding(ContentPresenter.ContentProperty, binding);

You need to bind to the Content property on the ContentPresenter, not DataContext, to apply the template to your data:

contentPresenter.SetBinding(ContentPresenter.ContentProperty, binding);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文