我正在尝试使用两个列表视图(在同一页面上 - wpf)来显示有关组类的信息。在第一个Listview中,我想显示组#和组大小(使用gridview),在第二个listview中,我想显示我在第一个listview框中选择的组中每个人的名字和姓氏。两个列表视图都使用网格视图。我有一个observableCollection,第一个listview绑定到。我是否要将另一个observablecollection绑定到第二个listview。如果是这样,我如何连接这两个集合?我无法弄清楚如何做到这一点。非常感谢任何帮助。
xaml:
<ListView HorizontalAlignment="Stretch" Margin="0,12" x:Name ="listViewGroups" ItemsSource="{Binding Groups}" IsSynchronizedWithCurrentItem="{x:Null}" Grid.Column="1">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding GroupNumber}" Width="40">
<GridViewColumnHeader Tag="GroupNumber" Content="#" Click="SortClick" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding GroupLeader}" Width="120">
<GridViewColumnHeader Tag="GroupLeader" Content="Group Leader" Click="SortClick" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding GroupSize}" Width="70">
<GridViewColumnHeader Tag="GroupSize" Content="Group Size" Click="SortClick" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Label Content="Leader" Height="28" Margin="12,12,0,0" Name="lblFirstName" Grid.Column="2" VerticalAlignment="Top" HorizontalAlignment="Left" />
<TextBox Text="{Binding SelectedItem.GroupLeader, ElementName =listViewGroups}" Height="23" Margin="12,31,0,0" Name="txtFirstName" MaxWidth="160" Grid.Column="2" VerticalAlignment="Top" HorizontalAlignment="Left" Width="160" />
<Label Content="Group Members" Height="28" HorizontalAlignment="Left" Margin="14,60,0,0" Name="label1" VerticalAlignment="Top" Grid.Column="2" />
<ListView HorizontalAlignment="Stretch" Margin="12,80,188,12" x:Name ="listViewGroupMembers" ItemsSource="{Binding Groups}" IsSynchronizedWithCurrentItem="{x:Null}" VerticalAlignment="Stretch" Grid.Column="2">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding FirstName}" Width="100">
<GridViewColumnHeader Tag="Name" Content="First Name" Click="SortClick" />
</GridViewColumn>
<GridViewColumn DisplayMemberBinding="{Binding LastName}" Width="80">
<GridViewColumnHeader Tag="Name" Content="Last Name" Click="SortClick" />
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
小组课程:
public class Group
{
public List<Camper> members;
private String name;
public static int groupCount = 0;
private static int optimalSize = 8;
public string GroupNumber { get; set; }
public string GroupLeader { get; set; }
public int GroupSize { get; set; }
public string FirstName { get { return "Testing"; } set { } }
public string LastName { get; set; }
public Group()
{
this.members = new List<Camper>();
this.name = "Group " + groupCount;
groupCount++;
}
}
在Mainwindow中:下面的点击事件与我制作的工具栏上的自定义按钮有关。当我单击按钮组是自定义的。
ObservableCollection<Group> bind = new ObservableCollection<Group>();
private void btnRun_Click(object sender, RoutedEventArgs e)
{
allGroups = AssignRelationshipValues.assignGroups(allCampers);
ObservableCollection<Camper> groupCampers = new ObservableCollection<Camper>();
bind.Groups.Clear();
for (int g = 0; g < allGroups.Count; g++)
{
for (int i = 0; i < allGroups[g].members.Count; i++)
{
groupCampers.Add(allGroups[g].members[i]);
} bind.Groups.Add(new Group { GroupNumber = allGroups[g].getName(), GroupSize = allGroups[g].getOptimalSize(), Members = groupCampers });
}
}
答案 0 :(得分:1)
如果我正确地阅读了这个内容,那么你想要的是你的第二个ListView
被这样绑定:
<ListView ItemsSource="{Binding SelectedItem.members, ElementName=listViewGroups}" />
与您TextBox
上绑定GroupLeader
的内容类似。另外,FirstName
和LastName
类不应该是Camper
类而不是Group
类的属性吗?如果我理解你所追求的是什么,他们就必须这样做才能发挥作用。
一些澄清:此绑定所说的是“转到'listViewGroups'ListView
并获取SelectedItem
”。这将是Group
对象,因为listViewGroups
绑定到Group
个对象的集合。然后,它会尝试在名为Group
的所选members
项目上找到一个属性,这是您的List<Camper>
- 您可能希望将其更改为ObservableCollection
。然后,第二个ListView
内的任何内容都将引用它所绑定的集合中的对象(Group.members
)来解析绑定,这意味着它将查找FirstName
和LastName
Camper
中的members
个对象。
您可以从MSDN获取有关Silveright / WPF数据绑定的进一步说明here。