动态绑定Code中的DataGridComboBoxColumn

时间:2013-11-16 04:53:51

标签: c# wpf xaml datagrid

我有一个带有一些datagridcombobox列的数据网格,我在xaml中将组合框列的绑定源设置为静态资源。但问题是我不知道如何重新绑定itemsSource以获取我所做的最新更改ItemSource。

我的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;
    }

}

我的问题是如何将代码中的DataGridComboBoxColumn而不是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 个答案:

答案 0 :(得分: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");
        }
}