2

我每隔几秒就会得到新的 ObservableCollection of Participants - 查看获取的更新都很好,问题是 SelectedItem ,当您从列表框中选择一个项目时,SelectedParticipant 会更新,但不是相反,我希望通过逻辑(每隔几个更新 ObservableCollection秒)选择我想要的项目(突出显示它),但它不起作用,它清除选择/在我设置 SelectedParticipant 后不显示选择/突出显示

  • 是的,我检查过 SelectedParticipant 不为空
  • 尝试过 LayoutUpdate() 或类似的东西
  • 尝试 UpdateSourceTrigger=PropertyChanged 在 SelectedItem="{Binding SelectedParticipant, Mode=TwoWay}"

谢谢

    private Participant _selectedParticipant;
    public Participant SelectedParticipant
    {
        get
        {
            return _selectedParticipant;
        }
        set
        {
            if (_selectedParticipant != value)
            {
                _selectedParticipant = value;

                RaisePropertyChanged("SelectedParticipant");
            }
        }
    }

    private ObservableCollection<Participant> _participants;
    public ObservableCollection<Participant> Participants
    {
        get
        {
            return _participants;
        }
        set
        {
            if (_participants != value)
            {
                _participants = value;

                RaisePropertyChanged("Participants");

                if (_participants != null && _participants.Count > 0)
                {
                    SelectedParticipant = null;

                    SelectedParticipant = Participants.FirstOrDefault(x => ... );

                }

            }
        }
    }

<ListBox ItemsSource="{Binding Participants}"
             SelectedItem="{Binding SelectedParticipant, Mode=TwoWay}"
             ItemContainerStyle="{StaticResource RedGlowItemContainer}" 
             ScrollViewer.HorizontalScrollBarVisibility="Disabled"  
             Background="Transparent" 
             Padding="25">
        <ListBox.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel />
            </ItemsPanelTemplate>
        </ListBox.ItemsPanel>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Border  BorderThickness="6" >
                    <Grid>
                       <Image Source="{Binding Client.ImageURL}"  VerticalAlignment="Center" HorizontalAlignment="Center" Stretch="Fill" Width="128" Height="128"/>
                    </Grid>
                </Border>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
4

1 回答 1

1

而不是为参与者分配值做清除和添加。这只是一个尝试

public class ViewModel
{
    public ObservableCollection<Participant> Participants { get; set; }

    public ViewModel()
    {
        Participants = new ObservableCollection<Participant>();
    }

    public void UpdateParticipants(IEnumerable<Participant> participants)
    {
        Participants.Clear();
        if (participants.Any())
        {
            foreach (var participant in participants)
            {
                Participants.Add(participant);
            }
            SelectedParticipant = Participants.First();
        }
    }
}

我希望这将有所帮助。

于 2013-08-08T08:49:35.087 回答