【发布时间】:2013-03-18 13:57:57
【问题描述】:
我正在尝试在文本块中获取要更新的总和,但是我只能通过重新启动 windows phone 模拟器来更新它。为什么会这样?
DisplayBill.xaml 中的代码
<TextBlock x:Name="lbTotalAmt" Text="{Binding Path=Sum, Mode=TwoWay, UpdateSourceTrigger=Explicit}" Margin="0,364,0,10" Grid.Row="1" />
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
更新
我想要做的是每次列表框添加项目时更新文本块。因此,每次将新项目添加到列表框中时,显示总金额的文本块都会更新。所以我的问题是如何在每次将新项目添加到列表框中时更新我的文本块?任何人都可以帮助我吗?我尝试使用下面的绑定表达式,但无济于事
public DetailPageBill()
{
InitializeComponent();
// Set the page DataContext property to the ViewModel.
this.DataContext = App.todoViewModel;
BindingExpression be = lbTotalAmt.GetBindingExpression(TextBlock.TextProperty);
be.UpdateSource();
}
【问题讨论】:
-
你能分享一个完整的复制品吗?
-
什么是完整复制?
-
@Keenlearner 调试时是否更改了“Sum”值?
-
是的,Sum确实变了,问题是重启手机模拟器后才反映
-
@Keenlearner 好的,当 Sum 设置时,您可以将鼠标放在 xaml 中的 Textblock 上吗? & 检查它是否显示您的更新值。
标签: c# data-binding windows-phone-7.1