Update automatic ListBox items when alter List

前端 未结 3 1512
南旧
南旧 2021-01-27 07:16

I have a ListBox, I populate it with ItemsSource with List.

But when I delete or add new control for this List, I need every tim

3条回答
  •  渐次进展
    2021-01-27 07:52

    In your Xaml, use something like this...

    
    

    And wire it up like this...

    public class ViewModel:INotifyPropertyChanged
        {
            public ObservableCollection MyItemsSource { get; set; }
            public ViewModel()
            {
                MyItemsSource = new ObservableCollection {new ListBox(), new TextBox()};
            }
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string name)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(name));
                }
            }
        }
    

    This will present the items to the ListBox. In the example here, the collection contains a ListBox and a TextBox. You can add/delete from the collection and get the behaviour you are after. Controls themselves are not all that great as ListBox items because they do not have a meaningful way of populating a visual. So you will probably need to run them through an IValueConverter.

提交回复
热议问题