简体   繁体   English

在ObservableCollection的某些元素上绑定DataGrid

[英]Bind a DataGrid on Certain element of an ObservableCollection

I am trying to bind a dataGrid with certain element of an ObservableCollection but my problem is when i add a row to my dataGrid all property of the ObservableCollection are displayed 我试图将dataGrid与ObservableCollection的某些元素绑定,但是我的问题是当我向dataGrid添加一行时,显示了ObservableCollection的所有属性

Here a picture of the dataGrid before i add rows : 这是我添加行之前的dataGrid图片: 在此处输入图片说明

Here a picture of the dataGrid with rows added : 这里是添加了行的dataGrid图片:

在此处输入图片说明

as you can see, columns are added to the datagrid 如您所见,列已添加到数据网格

xaml of the view : xaml的视图:

在此处输入图片说明

View Model : 查看模型:

  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();
            }
        }

The class : 班级 :

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
}

You need to set AutoGenerateColumns property to False on your dataGrid since you want to customise your columns. 由于要自定义列,因此需要在dataGrid上将AutoGenerateColumns属性设置为False By default its true. 默认情况下为true。

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

You have to set your DataGrid.AutoGenerateColumns property to false otherwise the grid will be generated automatically and your custom defined columns will be ignored. 您必须将DataGrid.AutoGenerateColumns属性设置为false否则网格将自动生成,并且您定义的自定义列将被忽略。

See also this post ;). 另请参阅此帖子 ;)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM