如何知道列表索引? [英] How to know the list index?

查看:85
本文介绍了如何知道列表索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPf中创建了一个边框列表,每个边框都有一个函数事件。

但是当我运行事件函数时,我不知道从列表中选择了哪个边框

我怎么知道发生的边界事件的索引?





I created a list of borders in WPf when each one has a function event.
But when I run the event function I do not know which number of border i chose from the list
how can i know the index of the border event that occurred?


//the bord list
 
        private  List<Border> bord;

 private Canvas piCanv;
 private void AddItem_Click(object sender, RoutedEventArgs e)
        {

            Grid gridborder = new Grid()
            {
                Width = 80,
                Height = 150

            };


             piCanv = new Canvas()
            {
                ToolTip = "Click here to add Image",
                Width = 75,
                Height = 120,
                Margin = new Thickness(1, 40, 1, 1),
                Background = new SolidColorBrush(Colors.LightCyan),

            };

            

            Label price = new Label()
            {
                Content =proud.ID+ "Price:"
            };
            TextBox priceEdit = new TextBox()
            {
                Width = 30,
                Height = 25,
                Margin = new Thickness(30, 1, 10, 130)
             
            };

            bord = new List<Border>();
             bord.Add(new Border()
            {
                Width = 100,
                Height = 200,
                CornerRadius = new CornerRadius(15),
                BorderThickness = new Thickness(5, 10, 15, 20),
                Margin = new Thickness(5, 5, 5, 5),
                BorderBrush = Brushes.SlateBlue,
                Child = gridborder,
                ToolTip="border"


            });


//the event 
            bord[bord.Count-1].MouseRightButtonDown += RemoveItem_Click;

            piCanv.MouseLeftButtonDown += piCanv_MouseLeftButtonDown;

            gridborder.Children.Add(piCanv);
            gridborder.Children.Add(price);
            gridborder.Children.Add(priceEdit);
            Warp.Children.Add(bord[bord.Count-1]);


        }

//the event function

private void RemoveItem_Click(object sender, RoutedEventArgs e)
      {
         
              bord = sender as List<Border>;

// here i want to know the index of border that i chose

             Warp.Children.Remove(bord[?]);


     




      }

推荐答案

要获得列表中项目的索引位置,您需要查看以下内容



To gain the index position of a item in a list you need to look at the following

 public class Test 
 {
  public int ID {get;set;}
  public string Name {get;set;}
 }


 List<test> SomeData = new List<test>();

 SomeData.Add(new Test {ID = 1, Name="Simon" });
 SomeData.Add(new Test {ID = 2, Name="Simon" });
 SomeData.Add(new Test {ID = 3, Name="Simon" });

 //Manually make the data that I am searching for
 //suitable to show how to find the index
 Test FindThis = new Test() {ID=2, Name="Simon" };

 int index = SomeData.IndexOf(FindThis);
</test></test>





进一步阅读 List(T).IndexOf [ ^ ]


有趣。第一:看起来每次运行AddItem_Click()时都会将bord列表重置为零。我想你要检查它是否为空,如果你真正想做的是'添加'到列表中。



秒:你有没有使用调试器来检查发件人是什么?我希望发送者是实际的列表项(Border),因为这是你将鼠标事件附加到的。



RemoveItem_Click的几个问题。在重新进行毛孔练习之前,你正在重复使用'bord'并消除其中的内容。

我还希望发件人真的应该是你想要从列表中删除的边框。



Interesting. First: it looks like every time you run AddItem_Click() you are resetting your bord list back to zero. I think you'd want to check if it's null if what you really want to do is 'Add' to the list.

second: Have you used the debugger to check what the sender is? I would expect sender to be the actual list item (Border), since that is what you attached the mouse event to.

several problem with the RemoveItem_Click. you are reusing 'bord' and wiping out what ever was in it before this is pore practice.
I also expect the Sender should really be the Border you are wanting to remove from the list.

private void RemoveItem_Click(object sender, RoutedEventArgs e)
      {
              bord = sender as List<Border>;

// here i want to know the index of border that i chose

             Warp.Children.Remove(bord[?]);

      }





所以将



So replace

bord = sender as List<Border>;

with



with

Border item = sender as Border;





然后



Warp.Children 。除去项目);



应该有效。你得到null,因为你试图将某些东西(边框)投射到它不是的东西(List< border>)



then

Warp.Children.Remove(item);

should work. you get null because you are trying to cast something (Border) to something that it isn't (List<border>)


这篇关于如何知道列表索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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