本文介绍了System.Windows.Controls.ListBoxItem:通过向列表框添加对象来不断出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这让我付出了沉重的代价。眼泪哈哈,它困扰了我好几天。 

This has cost me sweat & tears lol, it's been bothering me for days. 

问题:每当我添加一个对象时,该对象将出现"System.Windows.Controls.ListBoxItem:";在给定名称前面。这是因为我在我的代码中多次调用它... 

The problem: Everytime i add an object, the object will appear with "System.Windows.Controls.ListBoxItem:" in front of the given name. This is because i call it to read multiple times in my code.. 

////////////////////// //////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////

所以代码简化了:

       私人字符串WaardeListboxLezen(int IndexNummer)

        {

            ListBoxItem mySelectedItem = ListBox.Items [IndexNummer]作为ListBoxItem;



            if(mySelectedItem!= null)



            {

$
                return mySelectedItem.Content.ToString();
$


            }¥b $ b        }


        private void volgende_Click(object sender,RoutedEventArgs e)

        {

            int count = ListBox.Items.Count;

            if(index == count)

            {

                index = 0;

            }¥b $ b            string site = WaardeListboxLezen(index);

            index ++;

       }

       private string WaardeListboxLezen(int IndexNummer)
        {
            ListBoxItem mySelectedItem = ListBox.Items[IndexNummer] as ListBoxItem;

            if (mySelectedItem != null)

            {

                return mySelectedItem.Content.ToString();

            }
        }

        private void volgende_Click(object sender, RoutedEventArgs e)
        {
            int count = ListBox.Items.Count;
            if (index == count)
            {
                index = 0;
            }
            string site = WaardeListboxLezen(index);
            index++;
       }

推荐答案

你有一个ItemsSource应绑定到一个对象集合。  这将允许WPF为您创建ListBoxItems。  您还应该将ListBox的SelectedItem绑定到集合中保存的类型的属性。  然后
当您更改ListBox的选择时,此属性将更新,您可以执行任何处理,因为您将拥有所选项目。

You have an ItemsSource which should be bound to a collection of objects.  This will allow WPF to create the ListBoxItems for you.  You should also bind the SelectedItem of the ListBox to a property of the type that is held in the collection.  Then when you change the selection of the ListBox this property will be updated and you can do whatever processing you want as you will have the selected item.

如果您使用在代码中引用控件然后你应该知道你做错了。

If you use a reference to a control in your code then you should know you are doing it wrong.


这篇关于System.Windows.Controls.ListBoxItem:通过向列表框添加对象来不断出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 21:42