0

私はwpfが初めてなので、同様のトピックに関するWebページで自分自身を失っています。私が理解できなかったいくつかの基本的なことを誰かが説明してくれることを願っています.

Websocket を介してサーバーに接続された wpf アプリケーションがあります。サーバーは 5 秒ごとにリストを返します。すべての新しいリストは、古いリストとは何の関係もありません。新しいリストを取得すると、古いリストは重要ではなくなります。彼の ID に興味がある (リスト内の) Player の唯一のプロパティ。

どういうわけか、リストボックスを更新または更新する必要があります。観察可能なコレクションを次のように使用しました。

private static ObservableCollection<Player> sample;
private static List<Player> sample2 = new List<Player>();
public List<Player> update
{
   set
   {
   sample2 = value;
   sample = new ObservableCollection<Player>((List<Player>) sample2);      
   onPropertyChanged(sample, "ID");
   }
 }


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

propertychanged をデバッグするときは常に null です。ここで、リストボックスを更新する方法を本当に失いました。

リストボックスの xaml は次のようになります。

<DataTemplate x:Key="PlayerTemplate">
  <WrapPanel>
      <Grid >
        <Grid.ColumnDefinitions x:Uid="5">
          <ColumnDefinition  Width="Auto"/>
          <ColumnDefinition  Width="*"/>
          </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
          <RowDefinition Height="50"/>
          </Grid.RowDefinitions>

        <TextBlock VerticalAlignment="Center" Margin="5" Grid.Column="0" Text="{Binding Path=ID}" FontSize="22" FontWeight="Bold"/>                
      </Grid>                                  
    </WrapPanel>
4

1 に答える 1

1

sampleインスタンスではなくコレクションで"ID"あるため、呼び出されるプロパティがありません。さらに、コレクションを完全に置き換えているため、監視可能なものを使用しても意味がありません。これを試して:samplePlayer

private ICollection<Player> players = new List<Player>();

public ICollection<Player> Players
{
    get { return this.players; }
    private set
    {
        this.players = value;

        // the collection instance itself has changed (along with the players in it), so we just need to invalidate this property
        this.OnPropertyChanged(this, "Players");
    }
}
于 2012-06-11T10:08:47.003 に答える