为什么我的listview没有显示填充数据

时间:2015-04-27 12:30:57

标签: c# wpf mvvm

我需要使用数据库值填充listview,具体取决于已传递的按钮参数。这是我的情景。

   <Button x:Name="ViewFormOne" FontSize="14" Grid.Row="3" 
           Content="Form 1" Style="{StaticResource KwegaLinkButton}">
           <i:Interaction.Triggers>
               <i:EventTrigger EventName="Click">                                                
                   <cmd:EventToCommand Command="{Binding Path=ClassDetails}"
                       CommandParameter="{Binding Content, ElementName=ViewFormOne}" />                                                                                                
               </i:EventTrigger>
           </i:Interaction.Triggers>
          <Button.Triggers>
                <EventTrigger RoutedEvent="Button.LostMouseCapture">
                    <BeginStoryboard Storyboard="{StaticResource ShowFormOneData}"/>
                </EventTrigger>
         </Button.Triggers>
   </Button>

因此,当用户单击内容形式为1的按钮时,应更新驻留在其他用户控件上的列表视图并显示此数据。下面是viewmodel的代码。

private ObservableCollection<Student> formOneDetails;
public ObservableCollection<Student> FormOneDetails
   {
      get
        {
            return formOneDetails;
        }
      set
        {
            if (value == formOneDetails)
                 return;
                formOneDetails = value;
                OnPropertyChanged("FormOneDetails");
        }
    }

internal ObservableCollection<Student> FetchClassDetails(string detailed_class_name)

   {
     if (!StudentTableDoesntExist)
        {
          formOneDetails = new ObservableCollection<Student>();
          DataTable dtable = _student.GetFormOneStudents(detailed_class_name);

          if (dtable.Rows.Count > 0)
               {
                  foreach (DataRow row in dtable.Rows)
                      {
                         var values = row.ItemArray;
                         var student = new Student(studentID, firstName, lastName, email, className, streamName, studentCode, enrollmentNo,
                                phoneNo, emergencyContact, gender, student_nationality, dateOfBirth, dateOfJoin, profilePic, isDiscontinued,
                                isConfirmedByAdmin)
                            {
                                student_id = Convert.ToInt32(values[0]),
                                first_name = values[1].ToString(),
                                last_name = values[2].ToString(),
                                email = values[3].ToString(),
                                class_name = values[4].ToString(),
                                stream_name = values[5].ToString(),
                                student_code = values[6].ToString(),
                                enrollment_no = values[7].ToString(),
                                phone_no = values[8].ToString(),
                                emergency_contact = values[9].ToString(),
                                gender = values[10].ToString(),
                                nationality = values[11].ToString(),
                                date_of_birth = values[12].ToString(),
                                date_of_join = values[13].ToString(),
                                profile_pic = values[14].ToString(),
                                is_discontinued = Convert.ToBoolean(values[15]),
                                is_confirmed_by_admin = Convert.ToBoolean(values[16])
                            };
                            formOneDetails.Add(student);
                            ClassCount = formOneDetails.Count;
                        }
                    }            
                }
                return formOneDetails;
            }

    public ICommand ClassDetails
            {
                get
                {
                    if (classDetails == null)
                    {
                        classDetails = new CommandBase(i => FetchClassDetails(i as string), null);
                    }
                    return classDetails;
                }
            }

我将按钮名称作为参数传递给ClassDetails命令,并从数据库中获取详细信息。所有这些工作正常但formoneDetails没有从fetchclassdetails方法接收返回的值。其usercontrol上listview的数据绑定和datacontext看起来都很好,下面是代码。

 <ListView x:Name="FormOneView" ItemsSource="{Binding studentViewModel.FormOneDetails, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
                                  SelectedItem="{Binding studentViewModel.SelectedStudent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"                                   
                                  SelectedValue="{Binding parentViewModel.SelectedStudent}">                            

希望我的问题很清楚。

1 个答案:

答案 0 :(得分:0)

您没有设置您的财产。直接设置您的类成员会绕过setter,因此属性更改了通知。

您的表单永远不会收到通知。

FetchClassDetails根本不应修改任何类字段,而是使用它返回的 local 变量。

相反,您的命令应为:

classDetails = new CommandBase(i => FormOneDetails = FetchClassDetails(i as string), null);