从WPF中的ViewModel绑定嵌套列表视图

时间:2012-07-29 05:22:53

标签: wpf

我是WPF的初学者。我正在尝试从ViewModel绑定嵌套的listview。

我分别有两个listview父母和孩子。在我的父列表视图中,datatemplate想要绑定子列表视图

就像asp.net中的嵌套gridview一样。那里的任何人都可以帮我解决这个问题。

数据显示如:

学生ID名称

S101 Azad

数学3

C#3

此处StudentID,名称父列表视图内容和主题是子列表视图内容,按学生ID过滤。

谢谢,

AZ @ d

1 个答案:

答案 0 :(得分:1)

    <Grid>
    <ListView ItemsSource="{Binding Students}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="2"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"/>
                        <ColumnDefinition Width="5"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Name}" Grid.Column="0"/>
                    <TextBlock Text="{Binding RollNo}" Grid.Column="2"/>
                    <ListView ItemsSource="{Binding SubjectAndMarks}" Grid.Row="2" Grid.ColumnSpan="2" Grid.Column="0" BorderThickness="0">
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto"/>
                                        <ColumnDefinition Width="5"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="{Binding Subject}" Grid.Column="0"/>
                                    <TextBlock Text="{Binding Marks}" Grid.Column="2"/>
                                </Grid>
                            </DataTemplate>
                        </ListView.ItemTemplate>
                    </ListView>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

    public class SubjectAndMarks 
{
    public string Subject { get; set; }
    public double Marks { get; set; }
}
public class Student 
{
    public string Name { get; set; }
    public int RollNo { get; set; }
    public ObservableCollection<SubjectAndMarks> SubjectAndMarks { get; set; }
}
public class ViewModel //ViewModel
{
     public ObservableCollection<Student> Students { get; set; }
    public ViewModel()
    {
        Students = new ObservableCollection<Student>();
        Students.Add(new Student()
        {
            Name = "Harish",
            RollNo = 1,
            SubjectAndMarks = new ObservableCollection<SubjectAndMarks>()
        {new SubjectAndMarks(){Subject="Maths",Marks=100},new SubjectAndMarks(){Subject="Hindi",Marks=100},
        new SubjectAndMarks(){Subject="Science",Marks=100}}
        });
        Students.Add(new Student()
        {
            Name = "Pankaj",
            RollNo = 2,
            SubjectAndMarks = new ObservableCollection<SubjectAndMarks>()
        {new SubjectAndMarks(){Subject="Maths",Marks=100},new SubjectAndMarks(){Subject="Hindi",Marks=40},
        new SubjectAndMarks(){Subject="Science",Marks=30}}
        });
        Students.Add(new Student()
        {
            Name = "Deepak",
            RollNo = 3,
            SubjectAndMarks = new ObservableCollection<SubjectAndMarks>()
        {new SubjectAndMarks(){Subject="Maths",Marks=90},new SubjectAndMarks(){Subject="Hindi",Marks=50},
        new SubjectAndMarks(){Subject="Science",Marks=60}}
        });
    } 
}
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewModel();   
    }

我希望这会有所帮助。