私は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>