数据绑定设置为 null 并开始新的

发布于 2024-12-17 18:08:13 字数 1430 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

转身泪倾城 2024-12-24 18:08:13

如果我理解正确的话,您的 Order 对象就是 DataContext。如果您在代码隐藏中设置 DataContext,它看起来类似于:

[some_element].DataContext = myApp.Order;

您需要做的是将 DataContext 绑定 到 myApp.Order。这样,当您执行诸如

myApp.Order = new 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:

[some_element].DataContext = myApp.Order;

What you'd need to do is bind the DataContext to myApp.Order. This way, when you do something like

myApp.Order = new Order(...);

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.

北城挽邺 2024-12-24 18:08:13

几周前我也遇到了类似的问题。我发现,当将绑定属性分配给新对象时,绑定丢失了,因此作为解决方法,我必须创建一个新的临时对象,然后将所有字段复制到我的绑定属性中,这样您就有效地拥有了一个新对象和绑定被维持。

希望这有帮助!

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!

神仙妹妹 2024-12-24 18:08:13

也许将绑定更改为 UpdateSourceTrigger=PropertyChanged

Maybe change the binding to UpdateSourceTrigger=PropertyChanged?

人生戏 2024-12-24 18:08:13

如果 App 正确实现了 INotifyPropertyChanged,当您将 App.Order 设置为新实例时,它将触发,并且绑定将更新。

您确定它已正确实施吗?
例如。

class App
{
    public Order Order
    {
        get 
        {
            return _order;
        }
        set
        { 
            if (value != _order)
            {
                 _order = value;
                 FirePropertyChanged("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.

class App
{
    public Order Order
    {
        get 
        {
            return _order;
        }
        set
        { 
            if (value != _order)
            {
                 _order = value;
                 FirePropertyChanged("Order");
            }
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文