数据绑定设置为 null 并开始新的
我的 App 类中有一个对象 Order,两者都实现了 INotfiyPropertyChanged。 当订单结束时,我将其设置为 null 并执行 new Order() 来重新启动新订单。
问题是:似乎 DataContext 绑定到 Order 的对象似乎总是链接到旧的 Order
我可以做什么,以便在重新启动订单时不必再次手动重新绑定? 对于具有此 DataContext 的每个对象,我需要执行 object.DataContext= App.Order。我可以做什么来避免这种情况?
一些代码:
public partial class App : Application, INotifyPropertyChanged
{
private Order m_order = new Order();
public Order Order
{
get { return m_order; }
set
{
m_order = value;
NotifyPropertyChanged("Order");
}
}
//...
public bool getOrderClosed()
{
if (Order != null)
{
Order = null;
}
return (Order == null);
}
public bool getOrderOpened()
{
if (Order == null)
Order = new Order();
return (Order != null);
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
//code on the part where the order is finished
private void Confirm_Click(object sender, RoutedEventArgs e)
{
//...
if (SaveOrder())
{
theApp.getOrderClosed();
theApp.getOrderOpened();
theApp.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.Basket.DataContext = theApp.Order;
}
}
I have in my App class an object Order, both implementing INotfiyPropertyChanged.
When an Order is concluded, I set it to null and do a new Order() for restarting a new Order.
The problem is: it seems the objects that whose DataContext was bound to Order seems they are always linked to the older Order
What can I do to for not having to rebind again manually when I restart an Order?
For every object with this DataContext, I need to do object.DataContext= App.Order. What can I do to avoid this?
Some code:
public partial class App : Application, INotifyPropertyChanged
{
private Order m_order = new Order();
public Order Order
{
get { return m_order; }
set
{
m_order = value;
NotifyPropertyChanged("Order");
}
}
//...
public bool getOrderClosed()
{
if (Order != null)
{
Order = null;
}
return (Order == null);
}
public bool getOrderOpened()
{
if (Order == null)
Order = new Order();
return (Order != null);
}
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
//code on the part where the order is finished
private void Confirm_Click(object sender, RoutedEventArgs e)
{
//...
if (SaveOrder())
{
theApp.getOrderClosed();
theApp.getOrderOpened();
theApp.XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.Basket.DataContext = theApp.Order;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解正确的话,您的 Order 对象就是 DataContext。如果您在代码隐藏中设置 DataContext,它看起来类似于:
您需要做的是将 DataContext 绑定 到 myApp.Order。这样,当您执行诸如
[some_element] 的 DataContext 之类的操作时,也会发生变化。如果您在使用 Order 对象作为 DataContext 的位置发布 XAML 代码,我可以准确地向您展示 DataContext 上的绑定应该是什么样子。
通过您所做的操作,只有 DataContext 内的更改才会被拾取;当您更改正在使用的 DataContext 更改的属性时,DataContext 本身不会发生变化。
If I understand correctly, your Order object is the DataContext. If you're setting the DataContext in codebehind, it'd looks something like:
What you'd need to do is bind the DataContext to myApp.Order. This way, when you do something like
the DataContext for [some_element] will change as well. If you post your XAML code where you're using the Order object as the DataContext, I can show you exactly what your binding on the DataContext should look like.
With what you've done, only the changes within the DataContext will be picked up; when you change the property that you're using as the DataContext changes, the DataContext itself does not.
几周前我也遇到了类似的问题。我发现,当将绑定属性分配给新对象时,绑定丢失了,因此作为解决方法,我必须创建一个新的临时对象,然后将所有字段复制到我的绑定属性中,这样您就有效地拥有了一个新对象和绑定被维持。
希望这有帮助!
I had a similar problem a few weeks ago. I found that when assigning a bound property to a new object the binding was lost, so as a workaround I had to create a new temp object and then copy all the fields into my bound property, that way you effectivly have a fresh object and the binding is maintained.
Hope this helps!
也许将绑定更改为
UpdateSourceTrigger=PropertyChanged
?Maybe change the binding to
UpdateSourceTrigger=PropertyChanged
?如果 App 正确实现了 INotifyPropertyChanged,当您将 App.Order 设置为新实例时,它将触发,并且绑定将更新。
您确定它已正确实施吗?
例如。
If App implements INotifyPropertyChanged properly, it will fire when you set App.Order to a new instance, and the binding will update.
Are you sure it is implemented properly?
eg.