动态绑定CodeGridComboBoxColumn后面的代码 [英] Dynamically bind the DataGridComboBoxColumn in Code behind
问题描述
我的xaml是:
< Grid> ;
< Grid.Resources>
< ObjectDataProvider x:Key =ProductDataProviderObjectType ={x:Type local:clsPurchaseOrderList}MethodName =GetProducts/>
< /Grid.Resources>
< my:DataGrid Name =dgvPurchaseOrder
ItemsSource ={Binding}
SelectionUnit =CellOrRowHeader
TabIndex =3>
< my:DataGrid.Columns>
< my:DataGridComboBoxColumn
宽度=100
标题=产品代码
SelectedValueBinding ={Binding Path = Product_Id,UpdateSourceTrigger = PropertyChanged}
SelectedValuePath =Product_Id
DisplayMemberPath =Product_Code
ItemsSource ={Binding Source = {StaticResource ProductDataProvider}}>
< my:DataGridComboBoxColumn.EditingElementStyle>
< Style TargetType =ComboBox>
< Setter Property =IsEditableValue =True/>
< / Style>
< / my:DataGridComboBoxColumn.EditingElementStyle>
< / my:DataGridComboBoxColumn>
。
。
。
< / my:DataGrid.Columns>
< / my:DataGrid>
< / Grid>
我的代码是:
class clsPurchaseOrderList:INotifyPropertyChanged,IDataErrorInfo
{
//构造函数
public clsPurchaseOrderList()
{
GetProducts();
}
private int _Product_Id;
public int Product_Id
{
get {return _Product_Id; }
set
{
_Product_Id = value;
OnPropertyChanged(Product_Id);
}
}
//方法
public DataView GetProducts()
{
DataSet ds = new DataSet();
string qry =select PM.Record_Id as Product_Id,PM.Product_Code,PM.Product_Name,PTM.Product_Type from dbo.Tbl_Product_Master PM join dbo.Tbl_Product_Type_Master PTM on PTM.Record_Id = PM.Product_Category_Id其中PM.Is_Del = 0和PM.Is_Active = 1;
ds = ObjCommon.GetObject.ExecuteQuery_Select(Connection.ConnectionString,qry);
if(ds.Tables [0] .Rows.Count> 0)
return ds.Tables [0] .DefaultView;
else
返回null;
}
}
我的问题是如何绑定代码后面的DataGridComboBoxColumn而不是以xaml作为静态资源,因此我可以通过调用GetProducts()方法来动态地绑定ItemSource?
编辑:
我的业务对象是:
class clsPurchaseOrderList:INotifyPropertyChanged,IDataErrorInfo
{
public clsPurchaseOrderList()
{
GetProducts();
}
private int _Product_Id;
private ObservableCollection< Products> testCollection;
public int Product_Id
{
get {return _Product_Id; }
set
{
_Product_Id = value;
OnPropertyChanged(Product_Id);
}
}
public ObservableCollection< Products> TestCollection
{
get
{
return this.testCollection;
}
}
public void GetProducts()
{
DataSet ds = new DataSet();
DataTable testTable = new DataTable();
string qry =select PM.Record_Id as Product_Id,PM.Product_Code,PM.Product_Name,PTM.Product_Type from dbo.Tbl_Product_Master PM join dbo.Tbl_Product_Type_Master PTM on PTM.Record_Id = PM.Product_Category_Id其中PM.Is_Del = 0和PM.Is_Active = 1;
ds = ObjCommon.GetObject.ExecuteQuery_Select(Connection.ConnectionString,qry);
testTable = ds.Tables [0];
testCollection = new ObservableCollection< Products>();
foreach(testTable.Rows中的DataRow行)
{
var obj = new Products()
{
Product_Id =(int)row [ Product_Id],
Product_Code =(string)row [Product_Code],
Product_Name =(string)row [Product_Name],
Product_Type =(string)row [Product_Type ]
};
testCollection.Add(obj);
}
this.OnPropertyChanged(TestCollection);
}
}
我的xaml是:
< UserControl x:Class =RH_Inventory_Management_System.PURCHASING.Purchase_Order
xmlns =http://schemas.microsoft.com/winfx/ 2006 / xaml / presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml>
< Grid>
< my:DataGrid Name =dgvPurchaseOrder
ItemsSource ={Binding}
SelectionUnit =CellOrRowHeader
TabIndex =3>
< my:DataGrid.Columns>
< my:DataGridComboBoxColumn
宽度=100
标题=产品代码
SelectedValueBinding ={Binding Path = Product_Id,UpdateSourceTrigger = PropertyChanged}
SelectedValuePath =Product_Id
DisplayMemberPath =Product_Code
ItemsSource ={Binding Path = TestCollection,RelativeSource = {RelativeSource FindAncestor,AncestorType = {x:Type UserControl}}}>
< my:DataGridComboBoxColumn.EditingElementStyle>
< Style TargetType =ComboBox>
< Setter Property =IsEditableValue =True/>
< / Style>
< / my:DataGridComboBoxColumn.EditingElementStyle>
< / my:DataGridComboBoxColumn>
。
。
。
< / my:DataGrid.Columns>
< / my:DataGrid>
< / Grid>
我的代码是:
public partial class Purchase_Order:UserControl
{
ObservableCollection< clsPurchaseOrderList> lstItems;
public Purchase_Order()
{
InitializeComponent();
lstItems = new ObservableCollection< clsPurchaseOrderList>();
dgvPurchaseOrder.ItemsSource = lstItems;
}
}
尝试这个,
< Grid>
< my:DataGrid Name =dgvPurchaseOrder
ItemsSource ={Binding}
SelectionUnit =CellOrRowHeader
TabIndex =3>
< my:DataGrid.Columns>
< my:DataGridComboBoxColumn
宽度=100
标题=产品代码
SelectedValueBinding ={Binding
Path = Product_Id,
UpdateSourceTrigger = PropertyChanged}
SelectedValuePath =Product_Id
DisplayMemberPath =Product_Code
ItemsSource ={Binding Path = TestCollection,
RelativeSource = {RelativeSource FindAncestor,
AncestorType = {x:Type Window}}}}>
< my:DataGridComboBoxColumn.EditingElementStyle>
< Style TargetType =ComboBox>
< Setter Property =IsEditableValue =True/>
< / Style>
< / my:DataGridComboBoxColumn.EditingElementStyle>
< / my:DataGridComboBoxColumn>
。
。
。
< / my:DataGrid.Columns>
< / my:DataGrid>
< / Grid>
在您的代码中,使用observable集合来绑定您的组合框。
class clsPurchaseOrderList:INotifyPropertyChanged,IDataErrorInfo
{
private int _Product_Id;
public int Product_Id
{
get {return _Product_Id; }
set
{
_Product_Id = value;
OnPropertyChanged(Product_Id);
}
}
private ObservableCollection< ProductBO> testCollection;
public ObservableCollection< ProductBO> TestCollection
{
get
{
return this.testCollection;
}
}
public void GetProducts()
{
DataSet ds = new DataSet();
string qry =select PM.Record_Id as Product_Id,PM.Product_Code,PM.Product_Name,PTM.Product_Type from dbo.Tbl_Product_Master PM join dbo.Tbl_Product_Type_Master PTM on PTM.Record_Id = PM.Product_Category_Id其中PM.Is_Del = 0和PM.Is_Active = 1;
ds = ObjCommon.GetObject.ExecuteQuery_Select(Connection.ConnectionString,qry);
DataTable testTable = new DataTable();
testTable = ds.Tables [0];
testCollection = new ObservableCollection< ProductBO>();
foreach(testTable.Rows中的DataRow行)
{
var obj = new ProductBO()
{
Product_Code =(string)row [ Product_Code],
ProductNo =(int)row [ProductNo]
};
testCollection.Add(obj);
}
this.OnPropertyChanged(TestCollection);
}
}
I have a datagrid with some datagridcombobox column in it,I am setting the binding source of combobox column in xaml as Static Resource.But the problem is I dont know how to rebind the itemsSource to get the latest changes that I made to the ItemSource .
My xaml is:
<Grid >
<Grid.Resources>
<ObjectDataProvider x:Key="ProductDataProvider" ObjectType="{x:Type local:clsPurchaseOrderList}" MethodName="GetProducts" />
</Grid.Resources>
<my:DataGrid Name="dgvPurchaseOrder"
ItemsSource="{Binding}"
SelectionUnit="CellOrRowHeader"
TabIndex="3">
<my:DataGrid.Columns>
<my:DataGridComboBoxColumn
Width="100"
Header="Product Code"
SelectedValueBinding="{Binding Path=Product_Id,UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Product_Id"
DisplayMemberPath="Product_Code"
ItemsSource="{Binding Source={StaticResource ProductDataProvider}}">
<my:DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="IsEditable" Value="True" />
</Style>
</my:DataGridComboBoxColumn.EditingElementStyle>
</my:DataGridComboBoxColumn>
.
.
.
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
My code behind is:
class clsPurchaseOrderList : INotifyPropertyChanged, IDataErrorInfo
{
//Constructor
public clsPurchaseOrderList()
{
GetProducts();
}
private int _Product_Id;
public int Product_Id
{
get { return _Product_Id; }
set
{
_Product_Id = value;
OnPropertyChanged("Product_Id");
}
}
//Method
public DataView GetProducts()
{
DataSet ds = new DataSet();
string qry = "select PM.Record_Id as Product_Id,PM.Product_Code,PM.Product_Name,PTM.Product_Type from dbo.Tbl_Product_Master PM join dbo.Tbl_Product_Type_Master PTM on PTM.Record_Id=PM.Product_Category_Id where PM.Is_Del=0 and PM.Is_Active=1";
ds = ObjCommon.GetObject.ExecuteQuery_Select(Connection.ConnectionString, qry);
if (ds.Tables[0].Rows.Count > 0)
return ds.Tables[0].DefaultView;
else
return null;
}
}
My problem is how do I bind the DataGridComboBoxColumn in code behind instead of in xaml as static resource hence I can bind the ItemSource as Dynamically by calling the GetProducts() method?
Edit:
My business Object is:
class clsPurchaseOrderList : INotifyPropertyChanged, IDataErrorInfo
{
public clsPurchaseOrderList()
{
GetProducts();
}
private int _Product_Id;
private ObservableCollection<Products> testCollection;
public int Product_Id
{
get { return _Product_Id; }
set
{
_Product_Id = value;
OnPropertyChanged("Product_Id");
}
}
public ObservableCollection<Products> TestCollection
{
get
{
return this.testCollection;
}
}
public void GetProducts()
{
DataSet ds = new DataSet();
DataTable testTable = new DataTable();
string qry = "select PM.Record_Id as Product_Id,PM.Product_Code,PM.Product_Name,PTM.Product_Type from dbo.Tbl_Product_Master PM join dbo.Tbl_Product_Type_Master PTM on PTM.Record_Id=PM.Product_Category_Id where PM.Is_Del=0 and PM.Is_Active=1";
ds = ObjCommon.GetObject.ExecuteQuery_Select(Connection.ConnectionString, qry);
testTable = ds.Tables[0];
testCollection = new ObservableCollection<Products>();
foreach (DataRow row in testTable.Rows)
{
var obj = new Products()
{
Product_Id = (int)row["Product_Id"],
Product_Code = (string)row["Product_Code"],
Product_Name = (string)row["Product_Name"],
Product_Type = (string)row["Product_Type"]
};
testCollection.Add(obj);
}
this.OnPropertyChanged("TestCollection");
}
}
My xaml is :
<UserControl x:Class="RH_Inventory_Management_System.PURCHASING.Purchase_Order"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<my:DataGrid Name="dgvPurchaseOrder"
ItemsSource="{Binding}"
SelectionUnit="CellOrRowHeader"
TabIndex="3">
<my:DataGrid.Columns>
<my:DataGridComboBoxColumn
Width="100"
Header="Product Code"
SelectedValueBinding="{Binding Path=Product_Id,UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Product_Id"
DisplayMemberPath="Product_Code"
ItemsSource="{Binding Path=TestCollection,RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}}">
<my:DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="IsEditable" Value="True" />
</Style>
</my:DataGridComboBoxColumn.EditingElementStyle>
</my:DataGridComboBoxColumn>
.
.
.
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
My code behind is:
public partial class Purchase_Order : UserControl
{
ObservableCollection<clsPurchaseOrderList> lstItems;
public Purchase_Order()
{
InitializeComponent();
lstItems = new ObservableCollection<clsPurchaseOrderList>();
dgvPurchaseOrder.ItemsSource = lstItems;
}
}
Try this,
<Grid >
<my:DataGrid Name="dgvPurchaseOrder"
ItemsSource="{Binding}"
SelectionUnit="CellOrRowHeader"
TabIndex="3">
<my:DataGrid.Columns>
<my:DataGridComboBoxColumn
Width="100"
Header="Product Code"
SelectedValueBinding="{Binding
Path=Product_Id,
UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Product_Id"
DisplayMemberPath="Product_Code"
ItemsSource="{Binding Path=TestCollection,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type Window}}}}">
<my:DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="IsEditable" Value="True" />
</Style>
</my:DataGridComboBoxColumn.EditingElementStyle>
</my:DataGridComboBoxColumn>
.
.
.
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
And in your code behind, use an observable collection to bind your combobox.
class clsPurchaseOrderList : INotifyPropertyChanged, IDataErrorInfo
{
private int _Product_Id;
public int Product_Id
{
get { return _Product_Id; }
set
{
_Product_Id = value;
OnPropertyChanged("Product_Id");
}
}
private ObservableCollection<ProductBO> testCollection;
public ObservableCollection<ProductBO> TestCollection
{
get
{
return this.testCollection;
}
}
public void GetProducts()
{
DataSet ds = new DataSet();
string qry = "select PM.Record_Id as Product_Id,PM.Product_Code,PM.Product_Name,PTM.Product_Type from dbo.Tbl_Product_Master PM join dbo.Tbl_Product_Type_Master PTM on PTM.Record_Id=PM.Product_Category_Id where PM.Is_Del=0 and PM.Is_Active=1";
ds = ObjCommon.GetObject.ExecuteQuery_Select(Connection.ConnectionString, qry);
DataTable testTable = new DataTable();
testTable = ds.Tables[0];
testCollection = new ObservableCollection<ProductBO>();
foreach(DataRow row in testTable.Rows)
{
var obj = new ProductBO()
{
Product_Code= (string)row["Product_Code"],
ProductNo = (int)row["ProductNo "]
};
testCollection.Add(obj);
}
this.OnPropertyChanged("TestCollection");
}
}
这篇关于动态绑定CodeGridComboBoxColumn后面的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!