Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello

I don't understand why my binding do not work.

I have an XML file with a Binding Text :

<TextBlock TextWrapping="Wrap" Text="{Binding strSelectionLignes}" Grid.Column="1" Grid.Row="1" Grid.ColumnSpan="2" />


In my C# code, i have been use INotifyPropertyChanged :

public partial class ParamsAffichage : Window, INotifyPropertyChanged
    {
        #region Évenement qui permet de déclencher l'événement INotifyPropertyChanged et mettre à jour le binding
        public void Set<TValue>(ref TValue field, TValue newValue, [CallerMemberName] string propertyName = "")
        {
            if (EqualityComparer<TValue>.Default.Equals(field, default(TValue)) || !field.Equals(newValue))
            {
                field = newValue;

                if (PropertyChanged != null)
                    PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        private string _strSelectionLignes;
        public string strSelectionLignes
        {
            get { return _strSelectionLignes; }
            set { Set(ref _strSelectionLignes, value); }
        }


I modify my varaible strSelectionLignes :

 public ParamsAffichage()
        {
            InitializeComponent();

            strSelectionLignes = "TEST";
}


But "TEST" is not visible in my form

Why ?

Thank you very much

What I have tried:

I try in debug, in this test :

if (PropertyChanged != null)
                   PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));


PropertyChanged is alwys null.
Posted
Updated 24-Sep-17 20:26pm

1 solution

Of, sorry,,,,,,

I forgot to set datacontext = this ......
 
Share this answer
 
Comments
Graeme_Grant 25-Sep-17 3:00am    
Yes, for binding to work, you need to set the DataContext.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900