Wpf的comboBox怎么绑定数据?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Wpf的comboBox怎么绑定数据?相关的知识,希望对你有一定的参考价值。
Wpf的comboBox怎么绑定数据?数据是从数据库读取出来的,显示的是类别的名称,点击提交时提交的是类别的ID,这要怎么做?
WPF中提供了数据绑定的功能,操作起来很方便,集合类的控件几乎都可以用数据源来进行数据的绑定,下面操作一下下拉列表框控件ComboBox控件的数据绑定操作。要绑定到ComboBox控件的自定义类:
public class LocationRoad
public int ID set; get;
public string Code set; get;
public string Info set; get;
建立数据源,就将此数据集合当作数据源绑定到ComboBox:
///
/// 当ComboBox选中项更改时发生
///
private LocationRoad _selectLocation;
public LocationRoad SelectLocation
get
return this._selectLocation;
set
this._selectLocation = value;
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("SelectLocation"));
private ObservableCollection _locationRoad = null;
public ObservableCollection LocationSource
get
if (this._locationRoad == null)
this._locationRoad = new ObservableCollection()
new LocationRoad() ID = 1, Code = "NGQ", Info = "南岗区" ,
new LocationRoad() ID = 2, Code = "DLQ", Info = "道里区" ,
new LocationRoad() ID = 3, Code = "DWQ", Info = "道外区" ,
new LocationRoad() ID = 4, Code = "PFQ", Info = "平房区" ,
new LocationRoad() ID = 5, Code = "XFQ", Info = "香坊区" ,
;
return this._locationRoad;
set
this._locationRoad = value;
if (this.PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("LocationSource"));
参考技术A 把要帮定的值先从数据库里读出来,再放到集合里 循环读取就可以了
求采纳为满意回答。 参考技术B <ComboBox Name="fooBarComboBox"
ItemsSource="Binding Path=ExampleEnumsWithCaptions"
DisplayMemberPath="Value"
SelectedValuePath="Key"
SelectedValue="Binding Path=ExampleProperty, Mode=TwoWay" >
WPF Combobox数据绑定Binding
combobox数据绑定List链表集合区分显示值与选择的值
整体效果:
根据combobox选择情况分别打印选取值与显示值
代码:
Windows窗体:
1 <Window x:Class="ComboxBinding.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="ComBox绑定" Height="192.857" Width="385" WindowStartupLocation="CenterScreen" Loaded="Window_Loaded"> 5 <Grid> 6 <ComboBox Name="comBox1" HorizontalAlignment="Left" Margin="74,10,0,0" Width="209" Height="22" VerticalAlignment="Top"/> 7 <TextBlock Name="txtSelectedValue" Width="200" Text="{Binding ElementName=comBox1, Path=SelectedValue}" HorizontalAlignment="Left" Margin="115,58,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Background="#FFE7FBFA"/> 8 <TextBlock Name="txtSelectedText" Width="200" Text="{Binding ElementName=comBox1, Path=Text}" HorizontalAlignment="Left" Margin="114,88,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Background="#FFE7FBFA"/> 9 <Label Content="selectedValue" HorizontalAlignment="Left" Margin="2,58,0,0" VerticalAlignment="Top"/> 10 <Label Content="selectedText" HorizontalAlignment="Left" Margin="10,86,0,0" VerticalAlignment="Top"/> 11 12 </Grid> 13 </Window>
窗体cs代码
View Code
1 using System.Collections.Generic; 2 using System.Windows; 3 4 namespace ComboxBinding 5 { 6 /// <summary> 7 /// MainWindow.xaml 的交互逻辑 8 /// </summary> 9 public partial class MainWindow : Window 10 { 11 public MainWindow() 12 { 13 InitializeComponent(); 14 } 15 16 private void Window_Loaded(object sender, RoutedEventArgs e) 17 { 18 List<ComboxBind> lstCmbBind = new List<ComboxBind>();//用于绑定数据源 19 20 //初始化数据源 21 ComboxBind cbb = new ComboxBind("显示值1", "选取值1"); 22 lstCmbBind.Add(cbb); 23 cbb = new ComboxBind("显示值2", "选取值2"); 24 lstCmbBind.Add(cbb); 25 cbb = new ComboxBind("显示值3", "选取值3"); 26 lstCmbBind.Add(cbb); 27 28 this.comBox1.ItemsSource = lstCmbBind; 29 comBox1.DisplayMemberPath = "CmbText";//类ComboxBind中的属性 30 comBox1.SelectedValuePath = "CmbValue";//类ComboxBind中的属性 31 } 32 } 33 }
用于绑定combobox的类
1 namespace ComboxBinding 2 { 3 /// <summary> 4 /// 用于Combox数据绑定 5 /// </summary> 6 class ComboxBind 7 { 8 //构造函数 9 public ComboxBind(string _cmbText, string _cmbValue) 10 { 11 this.cmbText = _cmbText; 12 this.cmbValue = _cmbValue; 13 } 14 15 //用于显示值 16 private string cmbText; 17 public string CmbText 18 { 19 get { return cmbText; } 20 set { cmbText = value; } 21 } 22 23 //用于实际选取的值 24 private string cmbValue; 25 public string CmbValue 26 { 27 get { return cmbValue; } 28 set { cmbValue = value; } 29 } 30 } 31 }
以上是关于Wpf的comboBox怎么绑定数据?的主要内容,如果未能解决你的问题,请参考以下文章
WPF中的comboBox怎么绑定Datatable的数据源?
在WPF中怎么将ComboBox的下拉列表的数据进行绑定?还有能不能TXT文件中的列表?