chuck akers
chuck akers

Reputation: 59

I am trying to figure out whats wrong with my list

The only way I can describe whats happening is with an image so I hope this shows up, I think my problem is within the itemsource because I put these in a listbox and there is no blank spaces in the email list, in col1 it shows the names, and in col2 it shows the emails, but there not on the same rows, everything is pushed down.

here is the xaml

    <Window x:Class="test.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="492" Width="865" ResizeMode="CanMinimize">
<Grid>
    <Label Content="Name" Height="28" HorizontalAlignment="Left" Margin="12,12,0,0" Name="label1" VerticalAlignment="Top" IsEnabled="False" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="12,46,0,0" Name="textBox1" VerticalAlignment="Top" Width="180" />
    <Label Content="Email" Height="28" HorizontalAlignment="Left" Margin="12,138,0,0" Name="label3" VerticalAlignment="Top" IsEnabled="False" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="12,172,0,0" Name="textBox3" VerticalAlignment="Top" Width="180" />
    <Label Content="Website" Height="28" HorizontalAlignment="Left" Margin="12,75,0,0" Name="label4" VerticalAlignment="Top" IsEnabled="False" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="12,109,0,0" Name="textBox4" VerticalAlignment="Top" Width="180" />
    <Calendar Height="170" HorizontalAlignment="Left" Margin="12,235,0,0" Name="calendar1" VerticalAlignment="Top" Width="180" />
    <Label Content="Send Date" Height="28" HorizontalAlignment="Left" Margin="12,203,0,0" Name="label2" VerticalAlignment="Top" IsEnabled="False" />
    <TabControl Height="391" HorizontalAlignment="Left" Margin="225,14,0,0" Name="tabControl1" VerticalAlignment="Top" Width="606">
        <TabItem Header="My" Name="tabItem1" IsEnabled="True">
            <Grid>
                <ListView Height="359" ItemsSource="{Binding Tab1Collection}" HorizontalAlignment="Left" Name="listView1" VerticalAlignment="Top" Width="596">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn DisplayMemberBinding="{Binding name1}" Header="Name" Width="150" />
                            <GridViewColumn DisplayMemberBinding="{Binding email1}" Header="Email" Width="255" />
                            <GridViewColumn DisplayMemberBinding="{Binding website1}" Header="Website" Width="200" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>
        </TabItem>
        <TabItem Header="Finished" Name="tabItem2" IsEnabled="true">
            <Grid>
                <ListView Height="359" HorizontalAlignment="Left" Name="listView2" VerticalAlignment="Top" Width="596">
                    <ListView.View>
                        <GridView>
                            <GridViewColumn Header="Name" Width="150" />
                            <GridViewColumn Header="Email" Width="150" />
                            <GridViewColumn Header="Website" Width="150" />
                            <GridViewColumn Header="Delivered On" Width="150" />
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>
        </TabItem>
        <TabItem Header="Results" Name="tabItem3" IsEnabled="False">
            <Grid />
        </TabItem>
        <TabItem Header="Directory" Name="tabItem4" IsEnabled="true" IsSelected="True">
            <Grid Height="362"></Grid>
        </TabItem>
    </TabControl>
    <Button Content="Add Contact" Height="29" HorizontalAlignment="Left" Margin="97,411,0,0" Name="button1" VerticalAlignment="Top" Width="95" />
</Grid>

here is the code

    private List<string> names = new List<string>();
    private List<string> Emails = new List<string>();

    public MainWindow()
    {
        InitializeComponent();
        names.Add("test123");
        names.Add("test123");
        names.Add("test123");
        names.Add("tst123");
        names.Add("test123");
        names.Add("tst123");

        Emails.Add("[email protected]");
        Emails.Add("[email protected]");
        Emails.Add("[email protected]");
        Emails.Add("[email protected]");
        Emails.Add("[email protected]");
        Emails.Add("[email protected]");

        foreach(var n in names) {
            listView1.Items.Add(new {name1 = n});
        }

        foreach(var e in Emails) {
            listView1.Items.Add(new {email1 = e});
        }








    }

here is the image to explain whats happening, everything should be on the same row.

https://i.sstatic.net/4Uknl.png

Upvotes: 0

Views: 96

Answers (2)

Gonzalo.-
Gonzalo.-

Reputation: 12682

You're managing two different List, and you're not merging them, you're inserting one list, and then another one.

Considering two List have the same number of Items, you can do something Like

for(int index = 0; index < names.Count;index++)
{
     listView1.Items.Add(new {email1 = Emails[index], name1 = names[index] });
}

I don't remember the List syntax and properties, so if this doesn't work (or are better options) please edit me

Upvotes: 1

Icarus
Icarus

Reputation: 63964

You are adding 2 different collections to the listView by iterating through each of them separately so it's obvious that you'll get the effect you are seeing.

You need to create a class with 2 properties, name and email and then bind the ListView to this class. Something like:

public class Person
{
     public Person(string name,string email)
     { 
         Name=name;
         Email=email;
     }
     public string Name {get;set;}
     public string Email {get;set;}
}


List<Person> people = {new Person("name1","[email protected]"
                      ,new Person("name2","[email protected]"
                      ,new Person("name3","[email protected]"
                      ,new Person("name4","[email protected]")};

Now you can simply bind the listview as so:

listView1.ItemsSource=people;

Upvotes: 1

Related Questions