Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3613408
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:07:14+00:00 2026-05-18T22:07:14+00:00

I have created a class LeagueMatch . public class LeagueMatch { public string Home

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:07:15+00:00Added an answer on May 18, 2026 at 10:07 pm
    <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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a control that is created like so: public partial class MYControl :
I have created a class in which I have set some of it's fields
I have created a class Account . Next, I have created another class ReorderWindowController
I have created a class for a dashboard item which will hold information such
I have created UITableCellView class called NoteCell . The header defines the following: #import
I have created a class library in VB .NET. Some code in the library
I have created a C# class file by using a XSD-file as an input.
in my data layer class I have created a function to manually refresh the
At the company I work for, I have created a Error Logging class to
I have a collection of classes that inherit from an abstract class I created.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.