我能描述发生什么的唯一方法是使用图像,所以我希望这会出现,我认为我的问题在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});
}
}
这里是解释发生了什么的图像,一切都应该在同一行。
答案 0 :(得分:1)
您正在管理两个不同的List
,并且您没有合并它们,您正在插入一个列表,然后是另一个列表。
考虑到两个List
具有相同数量的Items
,您可以执行类似
for(int index = 0; index < names.Count;index++)
{
listView1.Items.Add(new {email1 = Emails[index], name1 = names[index] });
}
我不记得List
语法和属性,所以如果这不起作用(或者是更好的选项),请编辑我
答案 1 :(得分: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")};
现在您只需将listview绑定为:
listView1.ItemsSource=people;