0

我可以描述发生的事情的唯一方法是使用图像,所以我希望这会出现,我认为我的问题出在 itemsource 中,因为我将它们放在列表框中并且电子邮件列表中没有空格,在 col1 中它显示名称,并在 col2 中显示电子邮件,但不在同一行上,所有内容都被向下推。

这是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>

这是代码

    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("test123@Gmail.com");
        Emails.Add("test123@Gmail.com");
        Emails.Add("test123@Gmail.com");
        Emails.Add("test123@Gmail.com");
        Emails.Add("test123@Gmail.com");
        Emails.Add("test123@Gmail.com");

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

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








    }

这是解释发生了什么的图像,一切都应该在同一行。

http://i.stack.imgur.com/4Uknl.png

4

2 回答 2

1

您通过分别迭代每个集合来向 listView 添加 2 个不同的集合,因此很明显您将获得所看到的效果。

您需要创建一个具有 2 个属性的类,name然后email将其绑定ListView到此类。就像是:

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","test1@gmail.com"
                      ,new Person("name2","test2@gmail.com"
                      ,new Person("name3","test3@gmail.com"
                      ,new Person("name4","test4@gmail.com")};

现在您可以像这样简单地绑定列表视图:

listView1.ItemsSource=people;
于 2012-08-30T19:17:55.523 回答
1

您正在管理两个不同List的 ,并且您没有合并它们,您正在插入一个列表,然后插入另一个列表。

考虑到两个List具有相同数量的Items,您可以执行以下操作

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

我不记得List语法和属性,所以如果这不起作用(或更好的选择)请编辑我

于 2012-08-30T19:13:05.640 回答