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 6670291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:15:04+00:00 2026-05-26T03:15:04+00:00

I want to bind TextBlocks to a Modell. But it does not work and

  • 0

I want to bind TextBlocks to a Modell. But it does not work and I have no idea why.

class GameModel : INotifyPropertyChanged {
string[] _teamNames;
 ...
public string teamName(int team)
{
  return _teamNames[team];
}

public void setTeamName(int team, string name)
{
  _teamNames[team] = name;
  OnPropertyChanged("teamName");
}

protected void OnPropertyChanged(string name) {
 PropertyChangedEventHandler handler = PropertyChanged;
 if (handler != null)
  {
     handler(this, new PropertyChangedEventArgs(name));
  }
}

And the code which creates the TextBoxes

for (int currCol = 0; currCol < teams; currCol++) {
  TextBlock teamNameBlock = new TextBlock();
  Binding myNameBinding = new Binding();
  myNameBinding.Source = myGame;
  myNameBinding.Path = new PropertyPath("teamName", currCol);
  myNameBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
  teamNameBlock.SetBinding(TextBlock.TextProperty, myNameBinding); //The name of the team bind to the TextBlock
...
}
  • 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-26T03:15:04+00:00Added an answer on May 26, 2026 at 3:15 am

    Here’s a full, working example. The idea is to use an indexed property to access the team names.
    Note how the binding path is created: PropertyPath(“[” + currCol + “]”) , and how the property change is notified: OnPropertyChanged(“Item[]”);
    After the creation of controls, the name of the 3rd team is changed to “Marge” to test the binding.

    using System;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    
    namespace TestBinding
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();      
            }
    
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
                CreateTeamControls();
                myGame[2] = "Marge";
            }
    
            void CreateTeamControls()
            {
                var panel = new StackPanel();
                this.Content = panel;
                int teams = myGame.TeamCount;
    
                for (int currCol = 0; currCol < teams; currCol++)
                {
                    TextBlock teamNameBlock = new TextBlock();
    
                    panel.Children.Add(teamNameBlock);
    
                    Binding myNameBinding = new Binding();
                    myNameBinding.Source = myGame;
                    myNameBinding.Path = new PropertyPath("[" + currCol + "]");
                    myNameBinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                    teamNameBlock.SetBinding(TextBlock.TextProperty, myNameBinding);
                }
            }
    
            GameModel myGame = new GameModel();
        }
    }
    
    class GameModel : INotifyPropertyChanged 
    {
        string[] _teamNames = new string[3];
    
        public int TeamCount { get { return _teamNames.Count(); } }
    
        public GameModel()
        {
            _teamNames[0] = "Bart";
            _teamNames[1] = "Lisa";
            _teamNames[2] = "Homer";
        }
    
        public string this[int TeamName]
        {
            get
            {
                return _teamNames[TeamName];
            }
            set
            {
                if (_teamNames[TeamName] != value)
                {
                    _teamNames[TeamName] = value;
                    OnPropertyChanged("Item[]");
                }
            }
        }
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        private void OnPropertyChanged(string propertyName)
        {
            var changedHandler = this.PropertyChanged;
            if (changedHandler != null)
                changedHandler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some class with properties firstName and lastName. I want bind TextBlock to
I have a class in assembly AssemblyX with property Comment. I want to bind
i have an input element, and i want bind both change and keypress event
I want to bind an event to a certain class and ID for when
I have a datasource that I want to bind to a listview that has
I have TreeView control and I want to bind tree nodes' IsExpanded property to
I have an ObservableCollection<Object> that contains two different types. I want to bind this
I want to bind a textblock text to a property of a static class.
It's easy to bind something to the SelectedIndex of the ListBox, but I want
I have an ObservableCollection that I want to bind to my listbox... lbRosterList.ItemsSource =

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.