问题描述
我希望从数据库输出到组合框,
从数据库中检索数据但它没有绑定到组合框中
所有值都存储到可观察的收藏,但它没有约束力
这里我的代码
XAML
I wants get the out put from database into combo box,
retrieved data from the database but it's not bind into the combo box
all the values are stored into observable collection but it's not binding
here my Code
<StackPanel Orientation="Horizontal" DataContext="{Binding Path=LocationListModel, Mode=TwoWay}" >
<StackPanel Margin="5" >
<Label Content="Location Name"/>
<!--<TextBox TextWrapping="Wrap" Name="cmbLocation" Text="{Binding LocationNameLists, Mode=OneWay}" ></TextBox>-->
<ComboBox Name="cmbLocation" Grid.Column="1" Grid.Row="1" Width="200" ItemsSource="{Binding LocationNameList}" DisplayMemberPath="LocationName" Loaded="cmbLocation_Loaded">
<!--<ComboBoxItem Content="qwerty"/>-->
</ComboBox>
</StackPanel>
<StackPanel Margin="5">
<Label Content="Building Name"/>
<ComboBox Name="cmbBuilding" Width="200"/>
</StackPanel>
</StackPanel>
公共类LocationListModel:paymentModel
{
/ / LocationEntity locationEntity = new LocationEntity();
private ObservableCollection< paymententity> LocationList = new ObservableCollection< paymententity>();
// public ObservableCollection< paymententity> LocationId = new ObservableCollection< paymententity>();
public ObservableCollection< paymententity> LocationNameList
{
get {return LocationList; }
set
{
LocationList = value;
base.RaisePropertyChangedEvent(LocationNameList);
}
}
public ObservableCollection< ; paymententity> LocationIdList
{
get;
套;
}
public void locationListModel()
{
PaymentEntity paymentEntity = new PaymentEntity();
PaymentBussiness paymentBussienss =新的PaymentBussiness();
DataTable LocationNameList = paymentBussienss.GetLocationList();
foreach(LocationNameList.Rows中的DataRow行)
{
var obj = new PaymentEntity()
{
LocationId =(int)row.ItemArray [0],
LocationName =(string)row.ItemArray [1]
};
LocationList.Add(obj);
}
base.RaisePropertyChangedEvent(LocationNameList);
}
}
public class LocationListModel : paymentModel
{
//LocationEntity locationEntity = new LocationEntity();
private ObservableCollection<paymententity> LocationList = new ObservableCollection<paymententity>();
// public ObservableCollection<paymententity> LocationId = new ObservableCollection<paymententity>();
public ObservableCollection<paymententity> LocationNameList
{
get { return LocationList; }
set
{
LocationList = value;
base.RaisePropertyChangedEvent("LocationNameList");
}
}
public ObservableCollection<paymententity> LocationIdList
{
get;
set;
}
public void locationListModel()
{
PaymentEntity paymentEntity = new PaymentEntity();
PaymentBussiness paymentBussienss = new PaymentBussiness();
DataTable LocationNameList = paymentBussienss.GetLocationList();
foreach (DataRow row in LocationNameList.Rows)
{
var obj = new PaymentEntity()
{
LocationId = (int)row.ItemArray[0],
LocationName = (string)row.ItemArray[1]
};
LocationList.Add(obj);
}
base.RaisePropertyChangedEvent("LocationNameList");
}
}
推荐答案
<combobox name="cmbLocation" grid.column="1" grid.row="1" width="200"></combobox>
如果您在datlist中获取数据,那么您可以将这些数据绑定在这样的组合框中......
public void fillLocation()(当你想在组合框中绑定位置时调用这个方法)
{
cmbLocation.DataContext = dtSource;(你的数据表名称是什么)
cmbLocation.SelectedValuePath =ID;
cmbLocation.DisplayMemberPath =Name;
cmbLocation.UpdateLayout();
cmbLocation.SelectedValue = 0;
}
Happy Coding ............
If you are getting data in datlist then you can bind this data in combobox like that...
public void fillLocation()(Call this Method when you want to bind Location in combobox)
{
cmbLocation.DataContext = dtSource;(your data table name whatever it is)
cmbLocation.SelectedValuePath = "ID";
cmbLocation.DisplayMemberPath = "Name";
cmbLocation.UpdateLayout();
cmbLocation.SelectedValue = 0;
}
Happy Coding............
<Window x:Class="WpfApplication14.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication14"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel></local:ViewModel>
</Window.DataContext>
<Grid>
<StackPanel Orientation="Horizontal">
<StackPanel Margin="5" >
<Label Content="Location Name"/>
<ComboBox Name="cmbLocation" Grid.Column="1" Grid.Row="1" Width="200" ItemsSource="{Binding LocationNameList}">
</ComboBox>
</StackPanel>
<StackPanel Margin="5">
<Label Content="Building Name"/>
<ComboBox Name="cmbBuilding" Width="200"/>
</StackPanel>
</StackPanel>
</Grid>
</Window>
ViewModel.cs
ViewModel.cs
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
namespace WpfApplication14
{
class ViewModel : INotifyPropertyChanged
{
[AttributeUsageAttribute(AttributeTargets.Parameter, Inherited = false)]
public sealed class CallerMemberNameAttribute : Attribute { }
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
private ObservableCollection<string> locationNameList;
public ObservableCollection<string> LocationNameList
{
get
{
return locationNameList;
}
set
{
if (locationNameList != value)
{
locationNameList = value;
OnPropertyChanged();
}
}
}
public ViewModel()
{
locationNameList = new ObservableCollection<string>();
LocationNameList.Add("London");
LocationNameList.Add("Berlin");
LocationNameList.Add("Paris");
LocationNameList.Add("Mumbai");
LocationNameList.Add("Toronto");
}
}
}
这篇关于如何使用mvvm将数据从数据库保存到组合框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!