2017-09-14 5 views
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; 
    } 
} 

나는 서로 싸우는 플레이어, 어떤 수를 만들 적립 또는 전원을 잃게 :

나는 클래스 플레이어가 있습니다. 다각형을 사용하여, 자신의 파워 포인트 분포의 내가있는 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> 

나는 실행할 수있는 에포크를 선택할 수있는 컨트롤을 가지고 있으며, 실행 후리스트 뷰와 히스토그램은 잘 업데이트됩니다. 하지만 나는 100 만 에포크를 달리고 플레이어 포인트와 히스토그램의 변화를 실시간으로 모니터링 할 수 있기를 바란다. (예를 들어리스트와 그래프를 1000 에포크 나 매 초마다 업데이트한다. ..) 위 코드를 어떻게 변경해야합니까?

도움이 될 것입니다.

답변

0

당신은 속성 세터에서

을 NotifyPropertyChanged에 대한 호출을 제거하고 타이머가 설정 ElapsedTime 안타 후 수동으로 호출 할 수 있습니다.

타이머는 백그라운드 작업 스레드에있을 수 있으며 Dispatcher.BeginInvoke 블록 내에서 모든 알림을 수행합니다.

관련 문제