开发者

Binding a combobox to a database table

开发者 https://www.devze.com 2023-01-23 15:02 出处:网络
I have a combobox in my application , and I want to bind my combobox itemsource to a column in a table in my defined database

I have a combobox in my application , and I want to bind my combobox itemsource to a column in a table in my defined database

suppose the follwoing :

my database is called (Clincs) and the table is (Doctors) and the c开发者_运维百科olumn is (Names) and I want my combobox to retreive its items from this table and to get modified everytime the database is modified

Regards .


In XAML:

<Window x:Class="WpfApplication1.Window1">
  <ComboBox ItemsSource="{Binding Doctors}">
    <ComboBox.ItemTemplate>
      <DataTemplate>
        <TextBlock Text="{Binding Name}" />
      </DataTemplate>
    </ComboBox.ItemTemplate>
  </ComboBox>
</Window>

The code-behind:

 public ObservableCollection<Doctor> Doctors { get; set; }

 public Window1()
        {
            InitializeComponent();

            this.DataContext = this;

            Doctors = new ObservableCollection();

            LoadDoctors();
        }

        private void LoadDoctors()
        {
            Doctors.Clear();

            foreach (var doctor in DB.GetDoctors())
                Doctors.Add(doctor);
        }

Note that this does not address your requirement of automatically updating the collection when the table is modified, but hopefully this will get you started on the right track.

0

精彩评论

暂无评论...
验证码 换一张
取 消