Binding after data context is changed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • florin2905
    New Member
    • May 2013
    • 2

    Binding after data context is changed

    Hi,

    I have a strange problem.

    I have a WPF screen on which I have an extended TextBox that makes some conversions in some cases.That property is bound to a property on view model.

    When the data context is changed then the conversions are reevaluated. When reevaluated my extended TextBox has the new data context but the BindingOperatio ns.GetBindingEx pression(textBo x, dpproperty).Dat aItem is still having the old data context.

    Maybe the conversion that I make is too early after changing the data context?

    Usually the data context is not changed into the whole screen so the bindings to be reevaluated?

    Thanks!
    Last edited by Frinavale; May 30 '13, 01:46 PM. Reason: Fixed spelling and grammar. Formatted the post.
  • Sl1ver
    New Member
    • Mar 2009
    • 196

    #2
    I would put the data into a string. The way im doing it here means that the TextData is updated everytime something changes from either end(front or back).




    Code:
     private string textData;
            public string TextData
            {
                get { return textData; }
                set
                {
                    textData= value;
                    OnNotifyPropertyChanged("TextData");
                }
            }
    extend your class with
    Code:
    public class YourClassExtended : NotificationBase
    {
    }
    and create that NotificationBas e class
    Code:
    using System;
    using System.ComponentModel;
    using System.Windows;
    
    namespace StatsTool.AddIns.Models
    {
        public class NotificationBase : INotifyPropertyChanged
        {
            protected void OnNotifyPropertyChanged(string p)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(p));
                }
            }
    
            public bool IsDesignTime
            {
                get
                {
                    return (Application.Current == null) || (Application.Current.GetType() == typeof(Application));
                }
            }
    
            #region INotifyPropertyChanged Members
    
            public event PropertyChangedEventHandler PropertyChanged;
    
            #endregion
        }
    }

    Comment

    • florin2905
      New Member
      • May 2013
      • 2

      #3
      The bindings are refreshed into the whole tree when data context is changed.

      But in my case I have some bindings on the template of my extended TextBox.

      Here the DataContext is changed with some delay after the control where is used triggers the DataContext event, so the bindings are refreshed before the DataContext from my extended TextBox template is really changed, so the binding is still pointing to the old DataContext.
      Last edited by Frinavale; May 30 '13, 01:49 PM. Reason: Fixed spelling and grammar.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I've seen this happen in someone else's code before when they were not binding to the correct item.

        For example, they were supposed to be binding to the DataContext of the page, but they were actually binding to something like the DataContext of a parent element whose DataContext was the page.

        I'm not entirely sure what was happening, but the result was similar: the data context changed but the child element was still holding onto the old object.

        After I fixed the bindings, the problem went away.

        Your problem might be trickier than the one I fixed a few weeks ago.

        -Frinny

        Comment

        Working...