回调未正确绑定
在下面的代码中,UnitOccupierDetails 集合正确绑定,但 OwnersCountString 未正确绑定。谁能解释为什么?此代码位于我的 ViewModel 中:
private void BindSelectedStructure(object param)
{
UnitOccupierDetails.Clear();
Structure selectedStructure = (Structure)param;
this.SelectedStructure = selectedStructure;
int StructureID = selectedStructure.IDStructure;
loadOwners = context.Load<UserOccupier>(context.GetUnitOccupierDetailsQuery(StructureID), OwnersLoadedCallback, false);
}
private void OwnersLoadedCallback(LoadOperation<UserOccupier> op)
{
int Counter = 0;
foreach (var item in op.Entities)
{
Counter++;
UnitOccupierDetails.Add(item as UserOccupier);
}
OwnersCountString = "Owners(" + Counter.ToString() + ")";
}
以及 XAML:
<TextBlock Text='{Binding OwnersCountString,Source={StaticResource ViewModel},Mode=OneWay}'></TextBlock
OwnersCountStringProperty:
private string _ownersCountString;
public string OwnersCountString
{
get { return _ownersCountString; }
set { _ownersCountString = value; RaisePropertyChanged("OwnersCountString"); }
}
In the code below, the UnitOccupierDetails collection binds correctly but the OwnersCountString doesn't. Can anyone explain why? This code is in my ViewModel:
private void BindSelectedStructure(object param)
{
UnitOccupierDetails.Clear();
Structure selectedStructure = (Structure)param;
this.SelectedStructure = selectedStructure;
int StructureID = selectedStructure.IDStructure;
loadOwners = context.Load<UserOccupier>(context.GetUnitOccupierDetailsQuery(StructureID), OwnersLoadedCallback, false);
}
private void OwnersLoadedCallback(LoadOperation<UserOccupier> op)
{
int Counter = 0;
foreach (var item in op.Entities)
{
Counter++;
UnitOccupierDetails.Add(item as UserOccupier);
}
OwnersCountString = "Owners(" + Counter.ToString() + ")";
}
And the XAML:
<TextBlock Text='{Binding OwnersCountString,Source={StaticResource ViewModel},Mode=OneWay}'></TextBlock
OwnersCountStringProperty:
private string _ownersCountString;
public string OwnersCountString
{
get { return _ownersCountString; }
set { _ownersCountString = value; RaisePropertyChanged("OwnersCountString"); }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的回调未发生在 UI 线程上。这意味着更改正在发生,但 UI 尚未在显示更改的线程上更新自身。
这是我们的示例,来自我的另一个答案。我们的
SendPropertyChanged
版本(替换您的RaisePropertyChanged
)可确保所有代码都在 UI 线程上执行:Your callback is not occurring on the UI thread. That means that the change is happening, but the UI has not updated itself on the thread that displays changes.
Here is our example from another of my answers. Our version of
SendPropertyChanged
(replace yourRaisePropertyChanged
) makes sure any code is executed on the UI thread: