Wpf的comboBox怎么绑定数据?

Wpf的comboBox怎么绑定数据?,第1张

WPF中提供了数据绑定的功能, *** 作起来很方便,集合类的控件几乎都可以用数据源来进行数据的绑定,下面 *** 作一下下拉列表框控件ComboBox控件的数据绑定 *** 作。

要绑定到ComboBox控件的自定义类:

public class LocationRoad

{

public int ID { setget}

public string Code { setget}

public string Info { setget}

}

建立数据源,就将此数据集合当作数据源绑定到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"))

}

}

帮助别人就是帮助自己! 如果这里解决了您的问题,请您点一下推荐 × WPF项目中现有一个DataGrid,其中有一个名为“姓名”的模板列和名为“工号”的文本列,模板列在编辑时是一个ComBox,要实现的功能:ComBox的选项是从数据查询得到的姓名列表(如人员表中性别为女的人的姓名列表),选择其中的某个姓名,TextBlock显示选择的该姓名,“工号”列显示该姓名的工号,如何实现数据的绑定及将记录保存到数据库中<DataGrid AutoGenerateColumns="False" Height="200" HorizontalAlignment="Left" Margin="12,200,0,130" Name="dataGrid1" ItemsSource="{Binding}" Width="613">�0�2<DataGrid.Columns>�0�2<DataGridTemplateColumn x:Name="selectxmCol" Header="姓名" Width="100">�0�2<DataGridTemplateColumn.CellTemplate>�0�2<DataTemplate>�0�2<TextBlock Name="selectXm" Text="{Binding Path=xm}" ></TextBlock>�0�2</DataTemplate>�0�2</DataGridTemplateColumn.CellTemplate>�0�2<DataGridTemplateColumn.CellEditingTemplate>�0�2<DataTemplate>�0�2<Grid Name="selectxmGrid" FocusManager.FocusedElement="{Binding ElementName=xmComb}">�0�2<ComboBox Name="xmComb" DisplayMemberPath="xm" SelectedValuePath="gh" IsEditable="True" IsSynchronizedWithCurrentItem="True"></ComboBox>�0�2</Grid>�0�2</DataTemplate>�0�2</DataGridTemplateColumn.CellEditingTemplate>�0�2�0�2</DataGridTemplateColumn>�0�2�0�2<DataGridTextColumn Header="工号" Binding="{Binding Path=gh}" Width="100"/>�0�2</DataGrid.Columns>例如:Datatable data=new Datatable()首先你需要给你的Combox绑定待选的数据HTML code<DataGrid x:Name="gridEmployers" AutoGenerateColumns="False" ItemsSource="{Binding}" DataContext="{Binding}"><DataGridTemplateColumn.CellEditingTemplate><DataTemplate><ComboBox Text="{Binding Name}" IsEditable="True" DisplayMemberPath="Name" ItemsSource="{Binding Source={StaticResource employers}}" SelectionChanged="OnChangeNameSelection"/></DataTemplate></DataGridTemplateColumn.CellEditingTemplate><Window.Resources><ObjectDataProvider x:Key="employers" ObjectType="{x:Type local:_2011_12_28_01_Data}" MethodName="GetEmployers"/></Window.Resources>是用ObjectDataProvider来获取数据C# codepublicclass _2011_12_28_01_Data { public IEnumerable<Employer>GetEmployers() { returnnew Employer[]{ new Employer{ Name="A", Id=1}, new Employer{ Name="B", Id=2}, new Employer{ Name="C", Id=3}, new Employer{ Name="D", Id=4}, new Employer{ Name="E", Id=5} }} } 然后需要给Combox添加SelectionChanged事件:C# codeprivatevoid OnChangeNameSelection(object sender, SelectionChangedEventArgs e) { var data =this.FindResource("employers") as ObjectDataProvidervar employers = data.Data as IEnumerable<Employer>var cmb = sender as ComboBoxvar selectedItem =this.gridEmployers.SelectedItem as Employervar employer = employers.FirstOrDefault(x =>x.Name == (cmb.SelectedItem as Employer).Name)if (employer !=null) { selectedItem.Id = employer.IdselectedItem.Name = employer.Name} } 最后一点就是给你的数据实现INotifyPropertyChanged接口,否则看不到效果C# codepublicclass Employer : INotifyPropertyChanged { publicstring Name { get { return name} set { name = valueif (PropertyChanged !=null) { PropertyChanged(this, new PropertyChangedEventArgs("Name"))} } } publicstring namepublicint Id { get { return id} set { id = valueif (PropertyChanged !=null) { PropertyChanged(this, new PropertyChangedEventArgs("Id"))} } } publicint idpublicevent PropertyChangedEventHandler PropertyChanged}注意,DataGrid和Combox绑定的不一样给DataGrid绑定是在初始化随便加一个作为示例C# codepublic _2011_12_28_01() { InitializeComponent()this.DataContext =new Employer[] { new Employer { Name ="A", Id =1 } }} 2011-12-28 20:48 推荐: 0 次 有帮助? 请推荐 怎么木有人回答啊


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11658791.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-17
下一篇 2023-05-17

发表评论

登录后才能评论

评论列表(0条)

保存