C# - 如何实现包含ListViewItems的数据结构?

时间:2017-03-28 19:08:23

标签: c# wpf listview

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

之后,我需要创建一个on click事件,打开一个包含listviewitem(文章)详细信息的新窗口。我已经硬编码(如果这就是所谓的),因为我不知道如何更好地做到这一点。

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

我可能需要实现某种保持ListViewItems的数据结构,对吧?我该怎么做呢?

到目前为止我所拥有的:

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

1 个答案:

答案 0 :(得分:0)

我想在这个答案中使用MVVM,因为它几乎不需要WPF体验,不包括质疑你的生活选择。 MVVM代表Model,View,ViewModel。这样的事情应该让你开始......

首先是模型。这是您的数据结构。

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

然后,您在XAML中的窗口。这被视为您的观点。

<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。这将视图和模型绑定在一起。

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