DataTemplate/DataBinding not working.

<DataTemplate DataType="{x:Type l:Angle}">
<StackPanel Orientation="Horizontal">
<TextBox Text="{Binding Degrees}"/>
<TextBox Text="{Binding Minutes}"/>
<TextBox Text="{Binding Seconds}"/>
</StackPanel>
</DataTemplate>

<StackPanel Grid.Row="0">
<TextBlock>Northern Latitude:</TextBlock>
<ContentPresenter Content="{Binding NorthernLatitude}" />
<TextBlock>Western Longitude:</TextBlock>
<ContentPresenter Content="{Binding WesternLongitude}" />
</StackPanel>

This DataTemplate displays Angle object perfectly, however: when I edit one of the Angle properties and that particular textbox loses focus there is no update of the bound object. Even when I specify UpdateSourceTrigger=LostFocus there is no update.

I'm suspecting that the ContentPresenter is bugging me... Any suggestions



Answer this question

DataTemplate/DataBinding not working.

  • Rainki

    Yes it does:


        public class Angle : INotifyPropertyChanged
        {
            private int _degrees;

            public int Degrees
            {
                get { return _degrees; }
                set { _degrees = value; OnPropertyChanged("Degrees"); }
            }

            private int _minutes;

            public int Minutes
            {
                get { return _minutes; }
                set { _minutes = value; OnPropertyChanged("Minutes"); }
            }
            private double _seconds;

            public double Seconds
            {
                get { return _seconds; }
                set { _seconds = value; OnPropertyChanged("Seconds"); }
            }


            public Angle(int degrees, int minutes, double seconds)
            {
                _degrees = degrees % 360 + minutes / 60 + Convert.ToInt32(seconds) / 3600;
                _minutes = minutes % 60 + Convert.ToInt32(seconds) / 60;
                _seconds = seconds % 60;
            }

            public double ToDouble()
            {
                return _degrees + _minutes / 60.0 + _seconds / 3600;
            }

            public double ToRadians()
            {
                return (this.ToDouble() / 360.0) * (2 * Math.PI);
            }

            public static Angle GreenwichNorthernLatitude = new Angle(51, 28, 0);
            public static Angle GreenwichWesternLongitude = new Angle(0, 0, 0);

            #region INotifyPropertyChanged Members

            public event PropertyChangedEventHandler PropertyChanged;
            protected void OnPropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler e = PropertyChanged;
                if (e != null)
                {
                    e(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
        }
    }

     


  • Doooode

    Hmmm... solved it.

    The Angles that I used in the Bindings are properties of a class called CalendarSettings. After propagation of the PropertyChanged event from Angle to CalendarSettings it worked.

    Am I right in concluding that the Binding is NOT listening for changes of the property that it is bound to, but instead is listening for changes on the DataContext object


  • Kentmw

    Does the Angle type fire the correct notifications that its property has been modified through the INotifyPropertyChanged interface

    HTH,
    Drew


  • DataTemplate/DataBinding not working.