简体   繁体   中英

C# - how do I implement a data structure that contains ListViewItems?

So I'm doing a college project and my theme is web shop. What I have so far in C# is quite simple, I have a few ListViewItems (Articles) defined in WPF. Also when I change ComboBox index or click a button "New", other articles get inserted into my listview.

After that I needed to create an on click event that opens a new window with the listviewitem (article) details. I've hardcoded(if that's what it's called) this because I didn't know how to do it better.

The problem arises when I select a ComboBoxItem, which clears the listview and adds different items (articles) . When I click on the new articles, a window from the previous article opens when I click it.

I probably need to implement some kind of data structure that keeps ListViewItems, right? How would I approach doing this?

What I have so far:

namespace TRGOVINA
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void izhod(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void btnNovosti(object sender, RoutedEventArgs e)
        {
            string[] artikli = { "Intel procesor Core i7 6800K", "Intel procesor Core i5 7400", "AMD procesor Ryzen 7 1800X" };

            Artikel artikel1 = new Artikel();
            artikel1.Naziv = artikli[0];
            lvDataBinding.Items.Add(artikel1);

            Artikel artikel2 = new Artikel();
            artikel2.Naziv = artikli[1];
            lvDataBinding.Items.Add(artikel2);

            Artikel artikel3 = new Artikel();
            artikel3.Naziv = artikli[2];
            lvDataBinding.Items.Add(artikel3);
        }

        private void cbKlik_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            string[] izbira1 = { "Kingston 2, 5'' SSD disk 480 GB, SATA3", "DELL monitor LED UltraSharp U2412M", "Lenovo IdeaPad 110" };
            string[] izbira2 = { "PCX namizni računalnik Exam i5-7400/8GB/SSD120+1TB/Win10H", "Lenovo prenosnik V310", "Intel procesor Core i7-5820K" };
            string[] izbira3 = { "HP prenosnik Pavilion 17-ab004nm", "Intel procesor Core i7 6900K", "Gigabyte grafična kartica GTX 1080 OC" };
            string[] izbira4 = { "Asus prenosnik FX502VM-DM311T", "HP prenosnik Omen 17-w103nm", "DELL prenosnik Alienware 17" };

            ComboBox cmb = (ComboBox)sender;
            int izbranIndex = cmb.SelectedIndex;

            //lvDataBinding.Items.Clear();
            switch (izbranIndex)
            {
                case 0:
                    lvDataBinding.ItemsSource = null;
                    lvDataBinding.Items.Clear();
                    lvDataBinding.ItemsSource = izbira1;
                    break;
                case 1:
                    lvDataBinding.ItemsSource = null;
                    lvDataBinding.Items.Clear();
                    lvDataBinding.ItemsSource = izbira2;
                    break;
                case 2:
                    lvDataBinding.ItemsSource = null;
                    lvDataBinding.Items.Clear();
                    lvDataBinding.ItemsSource = izbira3;
                    break;
                case 3:
                    lvDataBinding.ItemsSource = null;
                    lvDataBinding.Items.Clear();
                    lvDataBinding.ItemsSource = izbira4;
                    break;
            }
          }

        private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //INSTEAD OF MESSAGEBOX A WINDOW HAS TO OPEN WITH LISTVIEW ITEM DETAILS - messagebox is just a placeholder
            int indeks = lvDataBinding.SelectedIndex;
            if (indeks == 0)
                MessageBox.Show(ime1.Text, "Naziv artikla", MessageBoxButton.OK, MessageBoxImage.Information);
            if (indeks == 1)
                MessageBox.Show(ime2.Text, "Naziv artikla", MessageBoxButton.OK, MessageBoxImage.Information);
            if (indeks == 2)
                MessageBox.Show(ime3.Text, "Naziv artikla", MessageBoxButton.OK, MessageBoxImage.Information);
            if (indeks == 3)
                MessageBox.Show(ime4.Text, "Naziv artikla", MessageBoxButton.OK, MessageBoxImage.Information);
            if (indeks == 4)
                MessageBox.Show(ime5.Text, "Naziv artikla", MessageBoxButton.OK, MessageBoxImage.Information);
            if (indeks == 5)
                MessageBox.Show(ime6.Text, "Naziv artikla", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void btnKosarica(object sender, RoutedEventArgs e)
        {
            Kosarica kosarica = new Kosarica();
            kosarica.Show();
        }
    }

    public class Artikel
    {
        public string Naziv { get; set; }

        public override string ToString()
        {
            return Naziv;
        }
    }
}

I'm goint to use MVVM in this answer since it's almost required for a WPF experience that doesn't include questioning your life choices. MVVM stands for Model, View, ViewModel. Something like this should get you started...

First is the Model. This is your data structure.

class Item
{
    public string Name { get; set; }
    public Item(string name)
    {
        Name = name;
    }
    public override string ToString()
    {
        return Name;
    }
}

Then, your window in XAML. This is considered your view.

<Window x:Class="Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>
    <StackPanel>
        <ListView ItemsSource="{Binding ElementName=_combobox, Path=SelectedItem}"/>
        <ComboBox x:Name="_combobox" ItemsSource="{Binding ItemLists}"/>
    </StackPanel>
</Window>

Last, this is what is called your ViewModel. This binds the View and Model together.

class ViewModel
{
    public List<List<Item>> ItemLists { get; private set; }
    public ViewModel()
    {
        string[] items = { "Intel procesor Core i7 6800K", "Intel procesor Core i5 7400", "AMD procesor Ryzen 7 1800X" };
        ItemLists = new List<List<Item>>();
        ItemLists.Add(new List<Item>());
        foreach (string item in items)
            ItemLists[0].Add(new Item(item));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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