Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8535379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:25:02+00:00 2026-06-11T10:25:02+00:00

I am trying to modify an existing code which allows to add articles by

  • 0

I am trying to modify an existing code which allows to add articles by rows and remove them wherever needed, the problem is that I am trying to find a way to group articles of the same category. So when the user adds a new article of DVD category, it will be directly added to that category (I tried to take some ideas from here, but with no success: http://msdn.microsoft.com/en-us/library/ff407126.aspx

This is the code behind data:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
namespace WpfDataGridWithDataTable
{
    public class Article
    {
        public  Article ()
        {

        }
        private int  _modelNumber;
        public int ModelNumber
        {
            get { return _modelNumber; }
            set { _modelNumber = value; OnPropertyChanged("ModelNumber"); }
        }

        private string _modelName;
        public string ModelName
        {
            get { return _modelName; }
            set { _modelName = value; OnPropertyChanged("ModelName"); }
        }

        private decimal  _unitCost;
        public decimal UnitCost
        {
            get { return _unitCost; }
            set { _unitCost = value; OnPropertyChanged("UnitCost"); }
        }

        private string  _description ;
        public string Description
        {
            get { return _description; }
            set { _description = value; OnPropertyChanged("Description"); }
        }


        #region INotifyPropertyChanged Membres

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
        #endregion
    }
    public class ListArticles : ObservableCollection<Article > 
    {
        public Article a;
        public ListArticles()
        {

                a = new Article();
                this.Add(a);

        }

    }

}

And here is the XAML code:

<Window x:Class="WpfDataGridWithDataTable.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfDataGridWithDataTable"
        Title="Window1" Height="300" Width="300">
    <Grid
        Name="gridPanel">
        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="40"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <DataGrid 
            Grid.Column="0"
            Name="dataGrid1"  
            AutoGenerateColumns="True"  
            CanUserAddRows="True" 
            CanUserDeleteRows="True"
            CanUserResizeColumns="True"
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding}"/>
        <ListBox 
            Grid.Column="1"
            Name="listBox1" 
            IsSynchronizedWithCurrentItem="True"
            ItemsSource="{Binding}">
            <ListBox.ItemTemplate>
                <DataTemplate DataType="{x:Type local:Article}">
                    <StackPanel    
                        Orientation="Horizontal">
                        <TextBlock 
                            Width="100"
                            Margin="10"  
                            Background="DarkBlue"
                            Foreground="White"
                            FontSize="14"
                            Text="{Binding ModelNumber}"/>
                        <TextBlock 
                            Width="100"
                            Margin="10" 
                            Background="DarkBlue"
                            Foreground="White"
                            FontSize="14"
                            Text="{Binding ModelName}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
            <Button 
                Grid.Row="1"
                Grid.Column="0"
                HorizontalAlignment="Left"
                Width="100"
                Name="btnAdd"
                Content="Add Item"
                Click="btnAdd_Click">
            </Button>
            <Button
                Grid.Row="1"
                Grid.Column="1"
                HorizontalAlignment="Right"
                Width="100"
                Name="btnDelete"
                Content="Delete Item"
                Click="btnDelete_Click" >
            </Button>
    </Grid>
</Window>

And the code behind the form:

namespace WpfDataGridWithDataTable
{
    /// <summary>
    /// Logique d'interaction pour Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        private ListArticles myList;
        public Window1()
        {
            InitializeComponent();
            myList = new ListArticles();


            this.DataContext = myList;

        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            myList.Add(new Article());
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            myList.Remove(this.dataGrid1.SelectedItem as Article);
        }

    }
}

Thanks for your answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:25:03+00:00Added an answer on June 11, 2026 at 10:25 am

    I finally resolved the problem, but I don’t know why it didn’t worked. The issue was located in the XAML code when binding the datagrid with the CollectionViewSource. I put:

     <DataGrid Name="dataGrid1"  
                AutoGenerateColumns="True"  
                CanUserAddRows="True" 
                CanUserDeleteRows="True"
                CanUserResizeColumns="True"
                IsSynchronizedWithCurrentItem="True"
                Grid.ColumnSpan="2" 
                ItemsSource="{Binding Source={StaticResource cvsListArticles}}">
    

    The last line was the problem, when I simply put ItemsSource="{Binding}" it finally worked.

    Here is the code for those who might have the same problem and want to benefit from it:

    code behind data:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Collections.ObjectModel;
    namespace WpfDataGridWithDataTable
    {
        public class Article
        {
            public Article()
            {
    
            }
            private int _modelNumber;
            public int ModelNumber
            {
                get { return _modelNumber; }
                set { _modelNumber = value; OnPropertyChanged("ModelNumber"); }
            }
    
            private string _modelName;
            public string ModelName
            {
                get { return _modelName; }
                set { _modelName = value; OnPropertyChanged("ModelName"); }
            }
    
            private decimal _unitCost;
            public decimal UnitCost
            {
                get { return _unitCost; }
                set { _unitCost = value; OnPropertyChanged("UnitCost"); }
            }
    
            private string _description;
            public string Description
            {
                get { return _description; }
                set { _description = value; OnPropertyChanged("Description"); }
            }
    
    
            #region INotifyPropertyChanged Membres
    
            public event PropertyChangedEventHandler PropertyChanged;
            private void OnPropertyChanged(string propName)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(propName));
                }
            }
            #endregion
        }
        public class ListArticles : ObservableCollection<Article>
        {
            public Article a;
            public ListArticles()
            {
                a = new Article();
                this.Add(a);
    
            }
    
    
        }
    
    }
    

    The XAML code:

    <Window x:Class="WpfDataGridWithDataTable.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:WpfApplication18"
            Title="Window1" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="480" Width="760">
    
         <Window.Resources>
                <CollectionViewSource x:Key="cvsListArticles" Source="Article">
    
                <CollectionViewSource.GroupDescriptions>
                    <PropertyGroupDescription PropertyName="ModelName"/>
                </CollectionViewSource.GroupDescriptions>
                </CollectionViewSource>
            </Window.Resources> 
    
    
        <Grid
            Name="gridPanel">
            <Grid.RowDefinitions>
                <RowDefinition Height="*"></RowDefinition>
                <RowDefinition Height="40"></RowDefinition>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <DataGrid Name="dataGrid1"  
                AutoGenerateColumns="True"  
                CanUserAddRows="True" 
                CanUserDeleteRows="True"
                CanUserResizeColumns="True"
                IsSynchronizedWithCurrentItem="True"
                ItemsSource="{Binding}" Grid.ColumnSpan="2" >
    
                <DataGrid.GroupStyle>
    
                    <GroupStyle>
                        <GroupStyle.ContainerStyle>
                            <Style TargetType="{x:Type GroupItem}">
                                <Setter Property="Margin" Value="0,0,0,5"/>
                                <Setter Property="Template">
                                    <Setter.Value>
                                        <ControlTemplate TargetType="{x:Type GroupItem}">
                                            <Expander IsExpanded="True" Background="#FF112255" BorderBrush="#FF002255" Foreground="#FFEEEEEE" BorderThickness="1,1,1,5">
                                                <Expander.Header>
                                                    <DockPanel>
                                                        <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0,0,0" Width="100"/>
                                                    </DockPanel>
                                                </Expander.Header>
                                                <Expander.Content>
                                                    <ItemsPresenter />
                                                </Expander.Content>
                                            </Expander>
                                        </ControlTemplate>
                                    </Setter.Value>
                                </Setter>
                            </Style>
                        </GroupStyle.ContainerStyle>
                    </GroupStyle>
                </DataGrid.GroupStyle>
            </DataGrid>
    
            <Button 
                    Grid.Row="1"
                    Grid.Column="0"
                    HorizontalAlignment="Left"
                    Width="100"
                    Name="btnAdd"
                    Content="Add Item"
                    Click="btnAdd_Click">
            </Button>
            <Button
                    Grid.Row="1"
                    Grid.Column="1"
                    HorizontalAlignment="Right"
                    Width="100"
                    Name="btnDelete"
                    Content="Delete Item"
                    Click="btnDelete_Click" >
            </Button>
            <Button Content="Group" Grid.ColumnSpan="2" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="294,5,0,0" Name="GroupButton" VerticalAlignment="Top" Width="145" Click="GroupButton_Click" />
        </Grid>
    </Window>
    

    The code behind the form:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    
    namespace WpfDataGridWithDataTable
    {
    
        public partial class Window1 : Window
        {
            private ListArticles myList;
            public Window1()
            {
                InitializeComponent();
    
    
    
                myList = new ListArticles();
    
    
                this.DataContext = myList;
    
            }
    
            private void btnAdd_Click(object sender, RoutedEventArgs e)
            {
                myList.Add(new Article());
            }
    
            private void btnDelete_Click(object sender, RoutedEventArgs e)
            {
                myList.Remove(this.dataGrid1.SelectedItem as Article);
            }
    
            private void GroupButton_Click(object sender, RoutedEventArgs e)
            {
                ICollectionView cvsListArticles = CollectionViewSource.GetDefaultView(dataGrid1.ItemsSource);
                if (cvsListArticles != null && cvsListArticles.CanGroup == true)
                {
                    cvsListArticles.GroupDescriptions.Clear();
                    cvsListArticles.GroupDescriptions.Add(new PropertyGroupDescription("ModelName"));
                }
            }
    
    
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to modify my existing code that returns from DB the rows
I'm trying to update (add/modify files) an existing JAR file, and this code (using
I'm trying to modify some existing JavaScript code which used apply() to pass an
I am trying to modify an existing PDF with iText. My code currently edits
i'm trying to modify a existing user and add a attribute to him. I'm
I was trying to modify an existing code to rotate an image based on
I am trying to modify an existing WPF application for localization. One of my
I'm trying to modify an existing NSIS install script to allow for different licence
I am trying to modify an existing MySQL database for use in a new
I am trying to modify an Integer field on existing table from nullable to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.