wpf 如何将 ComboBox 与 DataTable 绑定
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2749842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to bind ComboBox with DataTable
提问by Niko Gamulin
I have the DataTable with following columns:
我有包含以下列的数据表:
id, Name, Description, ParentId
ID、名称、描述、ParentId
and would like to create a WPF control (.NET 4.0 framework) which implements a combobox which displays the names which are bound to values of id. So when the user selects a name displayed in the combobox the behind logic has to retrieve its id value.
并想创建一个 WPF 控件(.NET 4.0 框架),它实现了一个组合框,该组合框显示绑定到 id 值的名称。因此,当用户选择组合框中显示的名称时,背后的逻辑必须检索其 id 值。
I would be very thankful if anyone could show the way of doing the described above.
如果有人可以展示执行上述操作的方式,我将非常感激。
回答by Omer Raviv
Like so:
像这样:
In your XAML file, put:
在您的 XAML 文件中,输入:
<ComboBox x:Name="myComboBox" DisplayMemberPath="Name" SelectedValuePath="id" />
In your code behind, put:
在你后面的代码中,输入:
myComboBox.ItemsSource = myTable;
(myTable being a reference to the table you mentioned)
(myTable 是对您提到的表格的引用)
Then you can reach the id of the currently selected person in the combo box using the expression:
然后,您可以使用以下表达式到达组合框中当前选定人员的 id:
NameComboBox.SelectedValue
回答by icernos
MVVM pattern solution
MVVM模式解决方案
XAML:
XAML:
<ComboBox
x:Name="myComboBox"
DisplayMemberPath="Name"
SelectedValuePath="id"
ItemsSource="{Binding myDataTable}"
SelectedValue="{Binding theID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
>
"Name" and "id" are columns in myDataTable.
“Name”和“id”是 myDataTable 中的列。
Code behind:
后面的代码:
private MyViewModel _myViewModel = new MyViewModel();
this.DataContext = _myViewModel;
MyViewModel class
MyViewModel 类
public DataTable myDataTable { get; set; }
public short theID { get; set; }
The selected value (row) under the "id" column gets assign to 'theID'.
“id”列下的选定值(行)被分配给“theID”。
回答by Jianghua Guo
private void InitCountry()
{
BasicData basicData = new DAL.BasicData();
DataTable CountryListDT = basicData.GetCountryList();
txtCountry.SelectedIndex = 0;
txtCountry.ItemsSource = CountryListDT.DefaultView;
}
private void txtCountry_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
BasicData basicData = new DAL.BasicData();
object obj = (object)e.AddedItems;
Int32 CountId = (Int32)txtCountry.SelectedValue;
InitProvince(CountId);
}
回答by Ryno Potgieter
My XAML:
我的 XAML:
<ComboBox Margin="10,0,0,0" x:Name="listStatus"
HorizontalAlignment="Left" Height="30" VerticalAlignment="Top" Width="200" SelectionChanged="listStatus_SelectionChanged"
DisplayMemberPath="Status" SelectedValuePath="StatusID" />
The Code Behind:
背后的代码:
private void Bind_StatusList()
{
Service1 serv = new Service1();
dtStatus = serv.GetStatuses(); // a WCF service getting the DataTable from a view from the database.
listStatus.ItemsSource = dtStatus.DefaultView;
}
This doesn't give a selected option when the window starts, but at least the list of Status is showing when I click the comboBox.
这不会在窗口启动时提供选定的选项,但至少当我单击组合框时会显示状态列表。