简体   繁体   English

数据绑定到文本块不会更新

[英]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. 我试图在文本块中获取更新的总和,但是我只能通过重新启动Windows Phone模拟器来更新总和。 Why is it so? 为什么会这样呢?

Code in DisplayBill.xaml 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 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的绑定的UpdateSourceTrigger设置为PropertyChanged

<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. 使用Explicit不会执行自动更新。 MSDN says: MSDN说:

Updates the binding source only when you call the UpdateSource method. 仅在调用UpdateSource方法时更新绑定源。

See MSDN on UpdateSourceTrigger for more information. 有关更多信息,请参见UpdateSourceTrigger上的MSDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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