在后面的代码中动态绑定 DataGridComboBoxColumn
Posted
技术标签:
【中文标题】在后面的代码中动态绑定 DataGridComboBoxColumn【英文标题】:Dynamically bind the DataGridComboBoxColumn in Code behind 【发布时间】:2013-11-16 04:53:51 【问题描述】:我有一个数据网格,其中包含一些 datagridcombobox 列,我将 xaml 中组合框列的绑定源设置为静态资源。但问题是我不知道如何重新绑定 itemsSource 以获得我所做的最新更改项目来源。
我的 xaml 是:
<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>
我的代码是:
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;
我的问题是如何在代码后面而不是在 xaml 中将 DataGridComboBoxColumn 绑定为静态资源,因此我可以通过调用 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 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");
我的 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
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>
我的代码是:
public partial class Purchase_Order : UserControl
ObservableCollection<clsPurchaseOrderList> lstItems;
public Purchase_Order()
InitializeComponent();
lstItems = new ObservableCollection<clsPurchaseOrderList>();
dgvPurchaseOrder.ItemsSource = lstItems;
【问题讨论】:
【参考方案1】:试试这个,
<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>
在您的代码中,使用可观察的集合来绑定您的组合框。
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");
【讨论】:
这只是示例代码。您需要创建您的属性类。 ,但是我在哪里调用 GetProducts() 方法 在你的构造函数中或者你可以在 Loaded 事件中设置。 我在对象构造函数中调用 GetProducts() 方法,并将 AncestorType=x:Type Window 更改为 AncestorType=x:Type UserControl bcz Iam 使用 Usercontrol 但组合框为空。请看我的编辑 就像,你的 Combobox 绑定 xaml 端一定有问题。我看不到你的编辑。你能告诉我你更新的 Xaml 代码吗?以上是关于在后面的代码中动态绑定 DataGridComboBoxColumn的主要内容,如果未能解决你的问题,请参考以下文章