将选定的列表框项目转换为列表C#

时间:2017-10-14 04:48:02

标签: c# listbox

我一直在尝试为学校项目编写虚拟商店,并且无法将所选项目转换为我的Shoppingcart列表

    public class shoppingcart
    {
        public int cost;
        public int id;
        public string name;

        public void setCost(int c)
        {
            cost = c;
        }
        public void setId(int i)
        {
            id = i;
        }
        public void setname(string n)
        {
            name = n;
        }

        public void getCost(int c)
        {

        }

        public void getId(int i)
        {

        }

        public void getname(string n)
        {

        }
    }
    public List<shoppingcart> Shoppingcart = new List<shoppingcart>();
    //An example of what my objects look like incase this helps
    //Experiment
        shoppingcart ghostitem = new shoppingcart();
        ghostitem.setname("");
        ghostitem.setCost(0);
        ghostitem.setId(0);
        Shoppingcart.Add(ghostitem);

    private void addCart_Click(object sender, EventArgs e)
    {
        try
        {
            totalitems = totalitems + 1;
            for (int i = 0; i < MeleeItem.Count(); i++)
            {
                Shoppingcart.Add(meleeList.SelectedItems);
            }
        }
        catch
        {

        }
    }

1 个答案:

答案 0 :(得分:0)

将ListBox ItemsSource属性绑定到shoppingCart项列表,并绑定到&#34; IsSelected&#34;每个ListBox项上的属性。

下面是一个简单的例子。 shopItems类有一个&#34; IsItemSelected&#34;和&#34;姓名&#34;用于绑定到ListBox的属性。在ListBox中选择项目时,IsItemSelected属性设置为true。

public partial class MainWindow : Window
    {
        List<shopItems> availableItems;
        public List<shopItems> AvailableItems
        {
            get
            {
                return availableItems;
            }
            set
            {
                availableItems = value;
            }
        }

        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            availableItems = new List<shopItems> { new shopItems { Name = "Item 1" }, new shopItems { Name = "Item 2" } };
        }

        public class shopItems
        {
            private string name;
            public string Name
            {
                get
                {
                    return name;
                }
                set
                {
                    name = value;
                }
            }

            private bool isItemSelected = false;
            public bool IsItemSelected
            {
                get
                {
                    return isItemSelected;
                }
                set
                {
                    isItemSelected = value;
                }
            }
        }
    }

XAML:

<ListBox ItemsSource="{Binding AvailableItems}" SelectionMode="Multiple" DisplayMemberPath="Name">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsSelected" Value="{Binding IsItemSelected}"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>