0
我希望能够在运行时显示一些属性更改,我不知道该怎么做。我正在学习绑定,下面的示例工作正常,但我希望能够在运行时监视绑定属性的更改。在运行时更新内容WPF
我有一类球员:
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;
}
}
我创建的球员,谁自相残杀一些数字,赚取或失去权力。使用点的功率分布的数据绑定我显示所有玩家的ID和电源点在ListView和直方图,使用面:
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>
我有控制,允许我选择运行多少个时代,并且在他们运行后,列表视图和直方图都很好地更新。但我希望能够运行一百万个时代,并且能够实时监控玩家点和直方图的变化(例如,每1000个时代或每秒钟更新列表和图表等) ..)我应该做什么更改上面的代码?
任何帮助,将不胜感激!