C#-如何实现包含ListViewItems的数据结构? [英] C# - how do I implement a data structure that contains ListViewItems?

查看:87
本文介绍了C#-如何实现包含ListViewItems的数据结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在做一个大学项目,我的主题是网上商店.到目前为止,我在C#中所拥有的非常简单,我在WPF中定义了一些ListViewItems(文章).另外,当我更改ComboBox索引或单击新建"按钮时,其他文章也会插入到我的列表视图中.

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.

此后,我需要创建一个on事件,该事件将打开一个包含listviewitem(文章)详细信息的新窗口.我已经对此进行了硬编码(如果这样的话),因为我不知道如何做得更好.

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.

当我选择一个ComboBoxItem时,就会出现问题,它会清除列表视图并添加不同的项目(文章).当我单击新文章时,单击时会打开上一篇文章的窗口.

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.

我可能需要实现某种保留ListViewItems的数据结构,对吗?我该怎么办呢?

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

推荐答案

我很想在此答案中使用MVVM,因为这几乎是WPF体验所必需的,而这不包括质疑您的生活选择. MVVM代表模型,视图,视图模型.这样的事情应该会让您入门...

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

然后,您在XAML中的窗口.这被认为是您的观点.

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>

最后,这就是所谓的ViewModel.这将视图和模型绑定在一起.

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

这篇关于C#-如何实现包含ListViewItems的数据结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆