c# / UWP 我可以将组合框绑定到可观察集合中的某个 "column " 吗

c# / UWP Can I bind a combobox to a certain "column " in an observable collection

我想将我的组合框绑定到我的可观察集合中的 "column"。

            private ObservableCollection<IUList> _ius = new ObservableCollection<IUList>();
                    public ObservableCollection<IUList> IUs
                    {
                        get
                        {
                            return _ius;
                        }
                        set
                        {
                            _ius = value;
                            RaisePropertyChanged("IUs");
                        }
                    }

            public class IUList
                    {

                        public string Identifier { get; set; }


                        public string SourceTrackNumber { get; set; }


                        public string TrackBlockStart { get; set; }


                        public string TrackBlockEnd { get; set; }

                        public IUList(string id, string stn, string tbs, string tbe)
                        {
                            this.Identifier = id;
                            this.SourceTrackNumber = stn;
                            this.TrackBlockStart = tbs;
                            this.TrackBlockEnd = tbe;

                        }

                    }

我希望我的组合框填充我的可观察集合中的所有 "Identifiers"。我只是不太清楚如何做到这一点。感谢任何帮助。

c# / UWP 我可以将组合框绑定到可观察集合中的某个 "column "

是的,这可以在 uwp/wpf 中使用数据绑定轻松完成。但是你必须仔细阅读ItemTemplate代码。

您可以这样编写 xaml 代码:

<ComboBox x:Name="comboBox">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Identifier}"/>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

然后在.cs代码中

        //Add data
        IUList list1 = new IUList("1", "1", "1", "1");
        IUList list11 = new IUList("11", "1", "1", "1");
        IUList list111 = new IUList("1111", "1", "1", "1");
        IUList list1111 = new IUList("11111", "1", "1", "1");

        ObservableCollection<IUList> ius = new ObservableCollection<IUList>();
        ius.Add(list1); ius.Add(list11); ius.Add(list111); ius.Add(list1111);

        //Bind source
        comboBox.ItemsSource = ius;

完成!然后你会看到

您也可以像这样编写组合框 xaml 代码:

<ComboBox x:Name="comboBox" Width="200" DisplayMemberPath="Identifier"/>