2013-08-08 3 views
2

Я получаю новый ObservableCollection участников каждые несколько секунд - просмотр настроек обновляет все хорошо, Проблема SelectedItem, SelectedParticipant обновляется, когда вы выбрали элемент из списка, но не другим способом, я хочу по логике (после того, как ObservableCollection обновляется каждые несколько секунд), чтобы выбрать элемент, который я хочу (выделите), но он не работает, он очищает выделение/не показывает выделение/выделение после установки SelectedParticipant мнойWPF mvvm light 4.5 ListBox SelectedItem двусторонняя привязка сломана

  • Да, я cheked Ответ конечно 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> 

ответ

1

Вместо Присвоение значений участникам делать четкие и Add.This просто a

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

Надеюсь, это поможет.

Смежные вопросы