首页 文章

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

提问于
浏览
2

我每隔几秒钟就会得到一个新的ObservableCollection参与者 - 查看获取更新的一切都很好,问题是SelectedItem,当你从列表框中选择一个项目时,SelectedParticipant会更新,但不是其他方式,我想通过逻辑(ObservableCollection每隔几个更新一次)秒)选择我想要的项目(突出显示),但它不起作用,它清楚选择/在我设置SelectedParticipant后不显示选择/突出显示

  • 是的,我被解雇的SelectedParticipant不是null

  • 尝试过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 回答

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

    我希望这将有所帮助 .

相关问题