在运行时WPF更新内容

时间:2017-09-14 10:33:30

标签: c# wpf data-binding runtime

我希望能够在运行时显示一些属性更改,我不知道该怎么做。我正在学习绑定,以下示例工作正常,但我希望能够在运行时监视绑定属性的更改。

我有一个班级玩家:

class Player : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string id;
    public string ID { get => id; }

    private int power;
    public int Power
    {
        get => power;
        set
        {
            if (this.power != value)
            {
                this.power = value;
                if (this.PropertyChanged != null)
                    this.NotifyPropertyChanged("Power");
            }
        }
    }


    public Player(string playerId)
    {
        id = playerId;
        power = 50;
    }
}

我创造了一些玩家,他们互相争斗并获得或失去权力。使用数据绑定我使用Polygon显示ListView中的所有玩家ID和功率点,以及他们的功率点分布的直方图:

public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private static readonly Random random = new Random();

        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }

        private ObservableCollection<Player> players;

        private PointCollection powerHistogramPoints;
        public PointCollection PowerHistogramPoints
        {
            get => this.powerHistogramPoints;
            set
            {
                if (this.powerHistogramPoints != value)
                {
                    this.powerHistogramPoints = value;
                    if (this.PropertyChanged != null) this.NotifyPropertyChanged("PowerHistogramPoints");
                }
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;

            players = new ObservableCollection<Player> { };
            for (int i = 1; i < 10; i++) players.Add(new Player("player" + i.ToString("D3")));
            popListView.ItemsSource = players;
            CreateHistogram();
        }

        private void runButton_Click(object sender, RoutedEventArgs e)
        {
            RunEpochs(Int32.Parse(nEpochsTextBox.Text));
            CreateHistogram();
        }

        private int[] CreateHistogram(...){...}

        internal void RunEpochs(int nEpochs)
        {for (int i = 0; i < nEpochs; i++) RunEpoch();}
        private void RunEpoch(){...}
    }

和xaml代码是:

        <ListView Margin="2" Name="popListView">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="player ID" Width="120" DisplayMemberBinding="{Binding ID}" />
                    <GridViewColumn Header="power" Width="50" DisplayMemberBinding="{Binding Power}" />
                </GridView>
            </ListView.View>
        </ListView>

           <GroupBox Height="200" Width="300" Header="Power Distribution" BorderThickness="0">
                <Border BorderThickness="1" BorderBrush="Black" Background="White" Margin="4">
                    <Polygon Points="{Binding PowerHistogramPoints}" Stretch="Fill" Fill="Black" Opacity="0.8" />
                </Border>
            </GroupBox>

我有控件允许我选择运行多少个纪元,并且在运行之后,listview和直方图都很好地更新。但我希望能够运行,比如一百万个时代,并且能够实时监控玩家点数和直方图的变化(例如,每1000个历元或每秒更新一次列表和图表,等等..)我应该对上述代码进行哪些更改?

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

您可以从属性Setters

中删除对NotifyPropertyChanged的调用

在你的计时器达到设定的ElapsedTime后手动调用它们。

Timer可以在后台工作线程中,并在Dispatcher.BeginInvoke块中进行所有通知。