C# 将数据集绑定到WPF中的组合框

C# 将数据集绑定到WPF中的组合框,c#,sql-server,wpf,C#,Sql Server,Wpf,我有个问题。我不是WPF大师,但我有一个家庭作业要完成 我正在尝试将数据库中的数据集绑定到组合框 我已经在Windows窗体应用程序中这样做了,但我不知道如何在WPF中这样做。 我在整个互联网上都搜索过,但我是个迟钝的人 如果你能帮助我,那就太好了 XAML: 数据库: category_select.ItemsSource = ds.Tables["t"].DefaultView; category_select.DisplayMemberPath = "description"; categ

我有个问题。我不是WPF大师,但我有一个家庭作业要完成

我正在尝试将数据库中的数据集绑定到组合框

我已经在Windows窗体应用程序中这样做了,但我不知道如何在WPF中这样做。 我在整个互联网上都搜索过,但我是个迟钝的人

如果你能帮助我,那就太好了

XAML:

数据库:

category_select.ItemsSource = ds.Tables["t"].DefaultView;
category_select.DisplayMemberPath = "description";
category_select.SelectedValuePath = "type_id";

如果列的名称是description和type_id,则只需执行以下操作:

category_select.DisplayMemberPath = "description";
category_select.SelectedValuePath = "type_id";
建议您在代码begind中设置ItemSource属性,而不是DataContext:

 category_select.ItemSource= ds.Tables["t"].DefaultView;
因此,您不必在视图中设置属性ItemsSource:

 <ComboBox Name="category_select"></ComboBox>
您也可以这样做:

 <ComboBox Name="category_select" DisplayMemberPath = "description" SelectedValuePath = "type_id"></ComboBox>

不要在代码中设置这些属性。

尝试删除defaultview获取,然后查找如何使用
 <ComboBox Name="category_select"></ComboBox>
 <ComboBox Name="category_select" DisplayMemberPath = "description" SelectedValuePath = "type_id"></ComboBox>