动态创建的绑定不起作用

时间:2011-09-30 13:50:32

标签: c# .net wpf binding

我想将TextBlocks绑定到Modell。但它不起作用,我不明白为什么。

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));
  }
}

创建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
...
}

2 个答案:

答案 0 :(得分:1)

我认为问题是你绑定到

public string teamName(int team)
{
  return _teamNames[team];
}

什么是team参数和变化的时刻?谁设置了该参数。 做这样的事情,而不是:

    public string teamName
    {
      get 
      {
            return _teamNames[currTeam];
      }
    }

绑定到属性,它返回基于currTeam索引的团队名称,该索引根据您的app逻辑进行设置。

希望这有帮助。

答案 1 :(得分:1)

这是一个完整的,有效的例子。我们的想法是使用索引属性来访问团队名称。 注意如何创建绑定路径:PropertyPath(“[”+ currCol +“]”),以及如何通知属性更改:OnPropertyChanged(“Item []”); 创建控件后,第3个团队的名称将更改为“Marge”以测试绑定。

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));
    }
}