简体   繁体   中英

Data binding to textblock does not update

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?

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

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? Can anyone help me please? I tried using the binding expression below but to no avail

public DetailPageBill()
    {
        InitializeComponent();

        // Set the page DataContext property to the ViewModel.
        this.DataContext = App.todoViewModel;

                BindingExpression be = lbTotalAmt.GetBindingExpression(TextBlock.TextProperty);
                 be.UpdateSource();                  

    }

Try setting UpdateSourceTrigger to PropertyChanged for your TextBlock 's binding:

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

With Explicit no automatic update is performed. MSDN says:

Updates the binding source only when you call the UpdateSource method.

See MSDN on UpdateSourceTrigger for more information.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM