1

我正在尝试将 dataGrid 与 ObservableCollection 的某些元素绑定,但我的问题是当我向 dataGrid 添加一行时,会显示 ObservableCollection 的所有属性

这是我添加行之前的dataGrid图片: 在此处输入图像描述

这是添加了行的 dataGrid 的图片:

在此处输入图像描述

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

视图的xml:

在此处输入图像描述

查看型号:

  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
}
4

2 回答 2

1

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

<DataGrid AutoGenerateColumns="False">
   .....
</DataGrid>
于 2013-10-20T19:36:47.987 回答
1

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

另见这篇文章;)。

于 2013-10-20T19:39:12.143 回答