wpf 为什么我的ObservableCollection没有触发CollectionChanged事件?

bogh5gae  于 2023-05-13  发布在  其他
关注(0)|答案(1)|浏览(219)

我有一个绑定到ComboBox的ObservableCollection,在程序中我向这个集合添加或删除元素。我希望这些都能反映在组合框中,但由于某种原因没有触发任何更改。我做错了什么?
组合框:

<ComboBox x:Name="Cbox2" ItemsSource="{Binding ExportChoices}" 
      Height="30" Width="200" Margin="10" FontSize="18" 
      HorizontalAlignment="Center" Grid.Column="1"/>

我从绑定的Observable Collection中删除项目的方法背后的代码:

public partial class ExportPage : Page
    {
        private ObservableCollection<String> exportChoices = new();

        public ObservableCollection<String> ExportChoices
        {
            get { return exportChoices; }
            set
            {
                exportChoices = value;
            }
        }

        public ExportPage()
        {
            InitializeComponent();

            this.DataContext = this;

            ExportChoices = new()
            {
                "Name",
                "Northing",
                "Easting",
                "Elevation",
                "Description"
            };
        }

    private void Cbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox comboBox = sender as ComboBox;
            switch (comboBox.Name)
            {
                case "Cbox1":
                    if (!ExportChoices.Contains(Cbox1Selection))
                    {
                        ExportChoices.Add(Cbox1Selection);
                    }
                    Cbox1Selection = comboBox.SelectedItem.ToString();
                    break;
                case "Cbox2":
                    if (!ExportChoices.Contains(Cbox2Selection))
                    {
                        ExportChoices.Add(Cbox2Selection);
                    }
                    Cbox2Selection = comboBox.SelectedItem.ToString();
                    break;
                case "Cbox3":
                    if (!ExportChoices.Contains(Cbox3Selection))
                    {
                        ExportChoices.Add(Cbox3Selection);
                    }
                    Cbox3Selection = comboBox.SelectedItem.ToString();
                    break;
                case "Cbox4":
                    if (!ExportChoices.Contains(Cbox4Selection))
                    {
                        ExportChoices.Add(Cbox4Selection);
                    }
                    Cbox4Selection = comboBox.SelectedItem.ToString();
                    break;
                case "Cbox5":
                    if (!ExportChoices.Contains(Cbox5Selection))
                    {
                        ExportChoices.Add(Cbox5Selection);
                    }
                    Cbox5Selection = comboBox.SelectedItem.ToString();
                    break;
            }

            if (ExportChoices.Contains(comboBox.SelectedItem.ToString()))
            {
                ExportChoices.Remove(comboBox.SelectedItem.ToString());
            }
        }
    }
7rtdyuoh

7rtdyuoh1#

原来我的ObservableCollection默认为我使用的导入中的一个类型,而不是典型的System.Collections类型。看来这就是为什么我没有得到一个集合更改事件。

相关问题