0

我是WPF的初学者。我正在尝试从 ViewModel 绑定嵌套列表视图。

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

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

数据显示如:

学生 ID 姓名

S101 阿扎德

数学 3

C# 3

这里 StudentID,Name parent listview content 和 subject 是由 StudentID 过滤的子 listview 内容。

谢谢,

阿兹@d

4

1 回答 1

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();   
    }

我希望这会有所帮助。

于 2012-07-29T07:20:49.513 回答