在ObservableCollection的某个元素上绑定DataGrid

时间:2013-10-20 19:19:35

标签: c# nhibernate data-binding mvvm

我试图用一个ObservableCollection的某个元素绑定一个dataGrid但我的问题是当我向我的dataGrid添加一行时,显示了ObservableCollection的所有属性

这是我在添加行之前的dataGrid图片: enter image description here

这里添加了一行dataGrid的图片:

enter image description here

如您所见,列被添加到数据网格

视图的xaml:

enter image description here

查看型号:

  public EntreeDeCommandeViewModel(IBanniereService banniereService
            , IItemsFactureService itemsFactureService
            , IFactureService factureService, INavigationService navigationService)
        {
            _banniereService = banniereService;
            _navigationService = navigationService;
            _itemsFactureService = itemsFactureService;
            _factureService = factureService;
            RetrieveArgs = new RetrieveArticleFactureArgs();

                AjouterItem = new RelayCommand(() =>
                {
                    List<ItemsFacture> _itFacture = new List<ItemsFacture>();
                    _itFacture.Add(new ItemsFacture() {NombreArticle = RetrieveArgs.NombreArticle
                        , PrixUnitaireArticle = RetrieveArgs.PrixArticle
                        , PrixTotalArticle = (RetrieveArgs.NombreArticle * RetrieveArgs.PrixArticle) });
                    ItemsFactures = new ObservableCollection<ItemsFacture>(_itFacture);
                });
        }

        public RetrieveArticleFactureArgs RetrieveArgs {get; set;}

        private ObservableCollection<ItemsFacture> _itemsFacture;

        public ObservableCollection<ItemsFacture> ItemsFactures
        {
            get
            {
                return _itemsFacture;
            }

            set
            {
                if (_itemsFacture == value)
                {
                    return;
                }

                RaisePropertyChanging();
                _itemsFacture = value;
                RaisePropertyChanged();
            }
        }

班级:

public class ItemsFacture : ObservableObject
{
    #region Membre
    private int? _idItemsFacture = null;
    private Facture _facture;
    private Article _article;
    private int? _nombreArticle;
    private double _prixUnitaireArticle;
    private double _prixTotalArticle;
    #endregion

    #region Propriete ItemsFacture
    public virtual int? IdItemsFacture
    {
        get
        {
            return _idItemsFacture;
        }

        set
        {
            if (_idItemsFacture == value)
            {
                return;
            }

            RaisePropertyChanging();
            _idItemsFacture = value;
            RaisePropertyChanged();
        }
    }

    public virtual Facture Facture
    {
        get
        {
            return _facture;
        }

        set
        {
            if (_facture == value)
            {
                return;
            }

            RaisePropertyChanging();
            _facture = value;
            RaisePropertyChanged();
        }
    }

    public virtual Article Article
    {
        get
        {
            return _article;
        }

        set
        {
            if (_article == value)
            {
                return;
            }

            RaisePropertyChanging();
            _article = value;
            RaisePropertyChanged();
        }
    }

    public virtual int? NombreArticle
    {
        get
        {
            return _nombreArticle;
        }

        set
        {
            if (_nombreArticle == value)
            {
                return;
            }

            RaisePropertyChanging();
            _nombreArticle = value;
            RaisePropertyChanged();
        }
    }

    public virtual double PrixUnitaireArticle
    {
        get
        {
            return _prixUnitaireArticle;
        }

        set
        {
            if (_prixUnitaireArticle == value)
            {
                return;
            }

            RaisePropertyChanging();
            _prixUnitaireArticle = value;
            RaisePropertyChanged();
        }
    }

    public virtual double PrixTotalArticle
    {
        get
        {
            return _prixTotalArticle;
        }

        set
        {
            if (_prixTotalArticle == value)
            {
                return;
            }

            RaisePropertyChanging();
            _prixTotalArticle = value;
            RaisePropertyChanged();
        }
    }
    #endregion
}

2 个答案:

答案 0 :(得分:1)

您需要在dataGrid上将AutoGenerateColumns属性设置为False,因为您要自定义列。默认为真。

<DataGrid AutoGenerateColumns="False">
   .....
</DataGrid>

答案 1 :(得分:1)

您必须将DataGrid.AutoGenerateColumns属性设置为false否则网格将自动生成,您的自定义列将被忽略。

另见post;)。