Updating a WPF List<> item from GUI

Nigel 271 Reputation points
2020-12-17T10:25:21.603+00:00

Hi I have created a ListGrid to display items in a list<> using XAML and C# in a WPF file. Within the list is a single bool item that denotes whether an item is in stock. However, I would like to enable this to be updated by the end user within the GUI rather than needing to be reprogrammed every time stock is exhausted but am not finding a means to transfer control for this to the end user.

 public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            List<Products> products = new List<Products>()
            {
                new Products{ID = 1, Name="HW-20HP", Description="Multi-band 20m - 6m OCFD", Type="Antenna", Price= 52.95, In_Stock = true },
                new Products{ID = 2, Name="HW-40HP", Description="Multi-band 40m - 6m OCFD", Type="Antenna", Price= 52.95, In_Stock=true },
                new Products{ID = 3, Name="HW-42HP", Description="Stacked OCFD multi-band, 40m - 6m ", Type="Antenna", Price= 59.95, In_Stock=true },
                new Products{ID = 4, Name="LW-10", Description="Multi-band Long Wire, 10m long for 40m - 6m", Type="Antenna", Price= 52.95, In_Stock = true },
                new Products{ID = 5, Name="LW-20", Description="Multi-band Long Wire, 20m long for 80m - 6m", Type="Antenna", Price= 52.95, In_Stock = true },
                new Products{ID = 6, Name="1:1 BALUN", Description="1:1 BALUN", Type="BALUN", Price= 39.00, In_Stock = true },
                new Products{ID = 7, Name="4:1 BALUN", Description="4:1 BALUN", Type="BALUN", Price= 39.00, In_Stock = true },
                new Products{ID = 8, Name="9:1 UNUN", Description="9:1 UNUN", Type="BALUN", Price= 39.00, In_Stock = true },
                new Products{ID = 9, Name="Magitenna", Description="Short multiband wire antenna", Type="Antenna", Price= 59.95, In_Stock = true },
                new Products{ID = 10, Name="VaxTenna", Description="Stacked dipoles 80, 40, 20, 15 and 10m", Type="Antenna", Price=84.95, In_Stock = true  }
            };


            ListGrid.ItemsSource = products;
        }


    }

    class Products
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public double Price { get; set; }
        public bool In_Stock { get; set; }
        public string Type { get; internal set; }
    }

Any help here would be appreciated.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,800 questions
0 comments No comments
{count} votes

Accepted answer
  1. DaisyTian-1203 11,626 Reputation points
    2020-12-18T06:01:05.353+00:00

    You can use MVVM to bind data to your UI, below is my update for you:

    Step 1: The code for cs:

      public partial class MainWindow : Window  
        {  
            public MainWindow()  
            {  
                InitializeComponent();  
                this.DataContext = new ViewModel();  
            }  
        }  
      
        public class ViewModel:NotifyObject  
        {  
            private Products selectedProduct;  
            public Products SelectedProduct  
            {  
                get { return selectedProduct; }  
                set   
                {   
                    selectedProduct = value;  
                    OnPropertyChange("SelectedProduct");  
                }  
            }  
            ObservableCollection<Products> products = new ObservableCollection<Products>();  
            public ObservableCollection<Products> Products  
            {  
                get { return products; }  
                set  
                {  
                    this.products = value;  
                    OnPropertyChange("Products");  
                }  
            }  
      
            public ViewModel()  
            {  
                Products = new ObservableCollection<Products>()  
                 {  
                     new Products{ID = 1, Name="HW-20HP", Description="Multi-band 20m - 6m OCFD", Type="Antenna", Price= 52.95, In_Stock = true },  
                     new Products{ID = 2, Name="HW-40HP", Description="Multi-band 40m - 6m OCFD", Type="Antenna", Price= 52.95, In_Stock=true },  
                     new Products{ID = 3, Name="HW-42HP", Description="Stacked OCFD multi-band, 40m - 6m ", Type="Antenna", Price= 59.95, In_Stock=true },  
                     new Products{ID = 4, Name="LW-10", Description="Multi-band Long Wire, 10m long for 40m - 6m", Type="Antenna", Price= 52.95, In_Stock = true },  
                     new Products{ID = 5, Name="LW-20", Description="Multi-band Long Wire, 20m long for 80m - 6m", Type="Antenna", Price= 52.95, In_Stock = true },  
                     new Products{ID = 6, Name="1:1 BALUN", Description="1:1 BALUN", Type="BALUN", Price= 39.00, In_Stock = true },  
                     new Products{ID = 7, Name="4:1 BALUN", Description="4:1 BALUN", Type="BALUN", Price= 39.00, In_Stock = true },  
                     new Products{ID = 8, Name="9:1 UNUN", Description="9:1 UNUN", Type="BALUN", Price= 39.00, In_Stock = true },  
                     new Products{ID = 9, Name="Magitenna", Description="Short multiband wire antenna", Type="Antenna", Price= 59.95, In_Stock = true },  
                     new Products{ID = 10, Name="VaxTenna", Description="Stacked dipoles 80, 40, 20, 15 and 10m", Type="Antenna", Price=84.95, In_Stock = true  }  
                 };  
            }  
             
        }  
      
      
        public class Products:NotifyObject  
        {  
            public int ID { get; set; }  
            public string Name { get; set; }  
            public string Description { get; set; }  
            public double Price { get; set; }  
      
            private bool in_Stock;  
            public bool In_Stock  
            {  
                get { return in_Stock; }  
                set  
                {  
                    in_Stock = value;  
                    OnPropertyChange("In_Stock");  
                }  
            }  
            public string Type { get; internal set; }  
        }  
      
      
        public class NotifyObject : INotifyPropertyChanged  
        {  
            public event PropertyChangedEventHandler PropertyChanged;  
      
            protected void OnPropertyChange(string propertyName)  
            {  
                if (PropertyChanged != null)  
                {  
                    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
                }  
            }  
      
        }  
    

    Step 2: Code for xaml:

    <StackPanel>  
            <DataGrid Name="ListGrid"   
                      HorizontalAlignment="Left"  
                      ItemsSource="{Binding Products}"   
                      AutoGenerateColumns="False"    
                      SelectionMode="Extended"   
                      SelectionUnit="FullRow"   
                      SelectedItem="{Binding SelectedProduct}"  
                      >  
                <DataGrid.Columns>  
                    <DataGridTextColumn Header="Id" Width="80" Binding="{Binding ID,Mode=TwoWay}"/>  
                    <DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name,Mode=TwoWay}"/>  
                    <DataGridTextColumn Header="Description" Width="50" Binding="{Binding Description,Mode=TwoWay}" />  
                    <DataGridTextColumn Header="Type" Width="50" Binding="{Binding Type,Mode=OneTime}" />  
                    <DataGridTextColumn Header="Price" Width="50" Binding="{Binding Price,Mode=TwoWay}" />  
                    <DataGridCheckBoxColumn Header="In_Stock" Width="50" Binding="{Binding In_Stock,Mode=TwoWay}" />  
                </DataGrid.Columns>  
            </DataGrid>  
            <WrapPanel>  
                <Label Content="IsIn_Stock:"></Label>  
                <CheckBox  Background="Azure"  IsChecked="{Binding SelectedProduct.In_Stock, Mode=TwoWay}"></CheckBox>  
            </WrapPanel>  
        </StackPanel>  
    

    Here is the result picture:
    49421-2.gif

    If I misundertand, please point out and give more details about your question for me to analyze.


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.