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.
精彩评论