2
votes

I get new ObservableCollection of Participants every few seconds - view gettings update all good, The problem is SelectedItem , SelectedParticipant is updated when you selected a item from the listbox , but not the other way , I want by logic ( after ObservableCollection is updated every few sec ) to select the item I wanna (highlight it) , but it doesn't work , its clear the selection / does't show selection/highlight after SelectedParticipant is set by me

  • Yes , ofcouse I cheked SelectedParticipant is not null
  • Tried LayoutUpdate() or something like that
  • Tried UpdateSourceTrigger=PropertyChanged inside SelectedItem="{Binding SelectedParticipant, Mode=TwoWay}"

Thanks

    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>
1

1 Answers

1
votes

Instead of Assigning Values to Participants do Clear and Add.This is just a try

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();
        }
    }
}

I hope this will help.