Jump to content
  • Advertisement
Sign in to follow this  
Theis_Bane

WPF DataBinding

This topic is 2117 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

OH THE GODS!!!! This problem has me ready to EAT THIS COMPUTER.

 

I have a character sheet designed in WPF that I am attempting to bind to a global Character object that holds all the information for the player's avatar.  Each TextBlock that displays info looks like this:

<TextBlock x:Name="tbkStrTotal" Grid.Row="1" Grid.Column="4" HorizontalAlignment="Right"
                       Foreground="White" Background="Black" Text="{Binding Strength}"

Because I wanted changes to my Character object to update my CharacterSheet window, I went through and implemented INotifyPropertyChanged.

Relevant code:

public class CharacterInfoModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private string m_Strength = "20";

        public string Strength
        {
            get
            {
                return m_Strength;
            }
            set
            {
                m_Strength = value;
                OnPropertyChanged("Strength");
            }
        }

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

Next, after initializing all of my windows, I hooked my CharacterInfoModel object to my CharacterSheet window's DataContext property.

public CharacterScreen()
        {
            InitializeComponent();
            this.DataContext = GameInfo.Character;
            
        }

The whole thing works great, until I try to change the value of Strength through code.  It won't update.  My PropertyChanged handler is always null, even though I thought it was supposed to get wired up automatically when I bound my CharacterInfoModel object to my CharacterSheet window's DataContext property.  Well, I think that's how it works.  I can't actually find anyone on the Google that can explain how it gets wired up, only that it NEEDS to be wired up.  So, halp.  Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement

The fact that it is a static may be the problem. I don't remember the exact semantics off the top of my hat, but statics have various pitfalls on the binding system.

Share this post


Link to post
Share on other sites
That was it. It took a bit of rearranging but I got it to work. I ended up making my Character object a non static member of a static manager object and that solved it. Thanks for pointing me it the right direction!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!