Domanda

I have a ListBox which contains a couple of TextBlocks, an Image, and at least 2 TextBoxs. However my problem is that I need to be able to retrieve all the TextBox(s) in the ListBox; APART FROM THE FIRST ONE, and then assign them to a List in C#.

Here is the ListBox in .xaml:

<ListBox Margin="0,-20,0,0" Height="548" Name="listBoxNew">
    <TextBlock Name="textBlockName" Text="Name"/>
    <TextBox Name="textBoxName" Width="420" Margin="-12,0,0,0"/>
    <TextBlock Name="textBlockAdd" Text="Add" Margin="0,10,0,0"/>
    <TextBox Name="textBoxAdd" Width="420" Margin="-12,0,0,0"/>
    <Image Name="imageAdd" Source="/SecondApp%2b;component/Images/buttonAdd1.png" 
           Height="50" Margin="0,5,0,0" Tap="imageAdd_Tap" 
           toolkit:TiltEffect.IsTiltEnabled="True" 
           ManipulationStarted="imageAddExersize_ManipulationStarted"  
           ManipulationCompleted="imageAddExersize_ManipulationCompleted" />
</ListBox>

The ListBox may have more TextBoxs than shown in .xaml, as the user can create more by tapping on the Image.

Thank alot, all help is appreciated.

È stato utile?

Soluzione

You can do it very simply using Linq. Following sentence returns all the elements from the ListBox of type TextBox except the first one:

var textBoxList = listBoxNew.Items.Where(x => x.GetType() == typeof(TextBox)).Skip(1).ToList();

Remember you have to add using System.Linq; to your file.

Altri suggerimenti

hello @Newbie i have solution for you..i know it is not optimized one..but it is working for your case.. i am comparing the types..here ( by not good way) ..i ma doing on a buttonclick..

List<object> lstobj;

    private void Button_Click_2(object sender, RoutedEventArgs e)
    {
        int t = listBoxNew.Items.Count();
        lstobj = new List<object>();
        TextBox obj = new TextBox();
        int p = 0;
        for (int i = 0; i < t; i++)
        {
            if(listBoxNew.Items[i].GetType()==obj.GetType())
            {
                if (p == 0)
                {
                    p = 1;
                    continue;
                }
                else
                {
                    lstobj.Add(listBoxNew.Items[i]);
                }
            }
        }

    }

hope it helps you..

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top