none
Data binding to textblock does not update RRS feed

  • Question

  • I am trying to get the get the sum to update in the textblock, however I'm only able to get it updated through restarting the windows phone emulator. Why is it so?

    Update

    What I'm trying to do is to update the textblock everytime the listbox adds an item. so everytime a new item is added into the listbox, the textblock which display the total amount will update. So my question is how do I go about updating my textblock everytime  a new item is added into the listbox

    Code in DisplayBill.xaml

    <TextBlock x:Name="lbTotalAmt" Text="{Binding Path=Sum, Mode=TwoWay, UpdateSourceTrigger=Explicit}" Margin="0,364,0,10" Grid.Row="1" />

    Code in ViewModel.cs

     private string _Sum;
        public string Sum
        {
            get {return _Sum;}
            set
            {
                _Sum = value;
                NotifyPropertyChanged("Sum"); 
            }
        } 
    
        #region INotifyPropertyChanged Members
    
        public event PropertyChangedEventHandler PropertyChanged;
    
        // Used to notify Silverlight that a property has changed.
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
    
            if (propertyName == "ToDoBills")
                UpdateSumValue();
        }
    
        private void UpdateSumValue()
        {
          Sum = ToDoBills.Sum(i => i.Amount).ToString();
        }
        #endregion


    • Edited by Bryan Liu JH Tuesday, March 19, 2013 11:30 AM To make it clearer
    Monday, March 18, 2013 2:49 PM

Answers

  • I just created a small sample and I cannot get it to fail even without an UpdateSource.

    All I did was create a button and a TextBlock.  I use the following for the TextBlock:

            <TextBlock Height="23" HorizontalAlignment="Left" 
                       Margin="140,22,0,0" Name="TextBlock1" 
                       Text="{Binding UpdateSourceTrigger=Explicit, Path=Sample, RelativeSource={RelativeSource FindAncestor, AncestorType=Window, AncestorLevel=1}}" 
                       VerticalAlignment="Top" Width="208" />
    

    The click event for the button simply changes the bound value "Sample" to a different string.  The update happens with no problem.

    And since I typed in all the above I just noticed what your problem is.  If you update Sum I am sure the value will be shown.  What you are doing is sending the ToDoBills as the property name for the Binding.  If you need to update both then in your code you need to raise both the propertychanged event for ToDoBills and Sum.  This should fix your problem.

    LS


    Lloyd Sheen

    Wednesday, March 20, 2013 6:04 PM

All replies

  • If you read the documentation for UpdateSourceTrigger you will see the following:

    If you set the UpdateSourceTrigger value to Explicit, you must call the UpdateSource method or the changes will not propagate back to the source.

     

    Lloyd Sheen

    Monday, March 18, 2013 3:09 PM
  • It still does not work even if I call the binding expression                

      public DetailPageBill()
            {
                InitializeComponent();
    
                // Set the page DataContext property to the ViewModel.
                this.DataContext = App.todoViewModel;
    
                        BindingExpression be = lbTotalAmt.GetBindingExpression(TextBlock.TextProperty);
                         be.UpdateSource();                  
    
    
    
    
    
            }

    Tuesday, March 19, 2013 11:07 AM
  • I just created a small sample and I cannot get it to fail even without an UpdateSource.

    All I did was create a button and a TextBlock.  I use the following for the TextBlock:

            <TextBlock Height="23" HorizontalAlignment="Left" 
                       Margin="140,22,0,0" Name="TextBlock1" 
                       Text="{Binding UpdateSourceTrigger=Explicit, Path=Sample, RelativeSource={RelativeSource FindAncestor, AncestorType=Window, AncestorLevel=1}}" 
                       VerticalAlignment="Top" Width="208" />
    

    The click event for the button simply changes the bound value "Sample" to a different string.  The update happens with no problem.

    And since I typed in all the above I just noticed what your problem is.  If you update Sum I am sure the value will be shown.  What you are doing is sending the ToDoBills as the property name for the Binding.  If you need to update both then in your code you need to raise both the propertychanged event for ToDoBills and Sum.  This should fix your problem.

    LS


    Lloyd Sheen

    Wednesday, March 20, 2013 6:04 PM