WPF mvvm light 4.5 ListBox SelectedItem双向绑定坏了

时间:2013-08-08 08:32:14

标签: wpf mvvm-light

我每隔几秒就会得到一个新的ObservableCollection参与者 - 查看获取更新的所有好处, 问题是SelectedItem,当您从列表框中选择一个项目时,SelectedParticipant会更新,但不是另一种方式,我希望通过逻辑(在每隔几秒更新ObservableCollection之后)来选择我想要的项目(突出显示它),但它没有工作,明确选择/在我设置SelectedParticipant之后不显示选择/突出显示

  • 是的,因为我被选中了SelectedParticipant不是空的
  • 尝试过LayoutUpdate()或类似的东西
  • 尝试过UpdateSourceTrigger =在SelectedItem内的PropertyChanged =“{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 个答案:

答案 0 :(得分: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();
        }
    }
}

我希望这会有所帮助。