如何让wpf窗口刷新?

时间:2021-01-11 07:24:08

I am building a simple roulette app. The player(UI) puts together a list of bets and submits them to the table object to be evaluated and paid out. I've got the code to work and the game process goes smoothly. The problem is that after a turn I can't get the player balance(textblock) or the betlist(listview) to update. Is there some sort of global window refresh command I am missing, or do I have to manually set each of these to update somehow?

我正在构建一个简单的轮盘赌应用程序。玩家(UI)将投注列表放在一起,并将它们提交给要评估和支付的表对象。我已经让代码工作,游戏过程顺利进行。问题是转弯后我无法获得玩家余额(文本块)或下注列表(listview)进行更新。是否有某种全局窗口刷新命令我缺少,或者我是否必须手动设置其中每一个以某种方式更新?

2 个解决方案

#1


WPF can take care of updating these values for you automatically, but you have to let it know when things have changed. Typically, this is done by using DependencyProperties on your model objects, but it can also be done by implementing INotifyPropertyChanged. In either case, when you update a property's value, the PropertyChanged event gets called; WPF automatically subscribes to this event when it binds to a value, and will update the UI when a change occurs. Without this notification, WPF won't check to see if the values in your object have changed, and you won't see the change reflected on the screen.

WPF可以自动为您更新这些值,但是当事情发生变化时您必须知道它。通常,这是通过在模型对象上使用DependencyProperties来完成的,但也可以通过实现INotifyPropertyChanged来完成。在任何一种情况下,当您更新属性的值时,都会调用PropertyChanged事件; WPF在绑定到某个值时自动订阅此事件,并在发生更改时更新UI。如果没有此通知,WPF将不会检查对象中的值是否已更改,并且您将看不到屏幕上反映的更改。

#2


What about implementing INotifyPropertyChanged, and bind the balance and the betlist to the controls you are using?

如何实现INotifyPropertyChanged,并将余额和betlist绑定到您正在使用的控件?

Something like:

public class Player : INotifyPropertyChanged
    {
        private int _balance;

        #region Properties

        public int Balance
        {
            get { return this._balance; }
            set
            {
                if (this._balance != value)
                {
                    this._balance = value;
                    NotifyPropertyChanged("Balance");
                }
            }
        }

        public BindingList<Bet> BetList { get; set; }

        #endregion // Properties

        private void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    public class Bet
    {
        // some code
    }

For the binding list you wouldn't need to implement anything since it implements an interface that notifies changes to whatever is bound to (IRaiseItemChangedEvents). But then again you could be using a different approach.

对于绑定列表,您不需要实现任何内容,因为它实现了一个接口,该接口通知对绑定到的任何内容(IRaiseItemChangedEvents)的更改。但话又说回来,你可能会采用不同的方法。

#1


WPF can take care of updating these values for you automatically, but you have to let it know when things have changed. Typically, this is done by using DependencyProperties on your model objects, but it can also be done by implementing INotifyPropertyChanged. In either case, when you update a property's value, the PropertyChanged event gets called; WPF automatically subscribes to this event when it binds to a value, and will update the UI when a change occurs. Without this notification, WPF won't check to see if the values in your object have changed, and you won't see the change reflected on the screen.

WPF可以自动为您更新这些值,但是当事情发生变化时您必须知道它。通常,这是通过在模型对象上使用DependencyProperties来完成的,但也可以通过实现INotifyPropertyChanged来完成。在任何一种情况下,当您更新属性的值时,都会调用PropertyChanged事件; WPF在绑定到某个值时自动订阅此事件,并在发生更改时更新UI。如果没有此通知,WPF将不会检查对象中的值是否已更改,并且您将看不到屏幕上反映的更改。

#2


What about implementing INotifyPropertyChanged, and bind the balance and the betlist to the controls you are using?

如何实现INotifyPropertyChanged,并将余额和betlist绑定到您正在使用的控件?

Something like:

public class Player : INotifyPropertyChanged
    {
        private int _balance;

        #region Properties

        public int Balance
        {
            get { return this._balance; }
            set
            {
                if (this._balance != value)
                {
                    this._balance = value;
                    NotifyPropertyChanged("Balance");
                }
            }
        }

        public BindingList<Bet> BetList { get; set; }

        #endregion // Properties

        private void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }

    public class Bet
    {
        // some code
    }

For the binding list you wouldn't need to implement anything since it implements an interface that notifies changes to whatever is bound to (IRaiseItemChangedEvents). But then again you could be using a different approach.

对于绑定列表,您不需要实现任何内容,因为它实现了一个接口,该接口通知对绑定到的任何内容(IRaiseItemChangedEvents)的更改。但话又说回来,你可能会采用不同的方法。