[英]ObservableCollection<> and ListView binding issue
我今天來是因為我不確定要了解有關C#中的綁定的一些知識。 我目前正在制作一個愚蠢的應用程序,以幫助自己了解自己的課程及其位置。 該應用程序中還包含其他一些信息。
所以我有這個設計,這是我的4個類的簡單ListView
。
MainPage.xaml:
<ContentPage.Content>
<AbsoluteLayout>
<Label
AbsoluteLayout.LayoutBounds="0.5, 0, 1, 0.1"
AbsoluteLayout.LayoutFlags="All"
Text="My CSULB Schedule" />
<ListView x:Name="CoursesListView" ItemsSource="{Binding Courses}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="2*" />
<RowDefinition Height="6*" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
<!-- TOP -->
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="2*" />
<ColumnDefinition Width="6*" />
<ColumnDefinition Width="2*" />
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
HorizontalTextAlignment="Center"
Text="{Binding DaysToString}"
VerticalTextAlignment="Center" />
<Label
Grid.Column="1"
HorizontalTextAlignment="Center"
Text="{Binding Title}"
VerticalTextAlignment="Center" />
<Label
Grid.Column="2"
HorizontalTextAlignment="Center"
Text="{Binding TypeAndNumber}"
VerticalTextAlignment="Center" />
</Grid>
<!-- Top -->
<!-- Center -->
<ListView Grid.Row="1" ItemsSource="{Binding Sections}">
<ListView.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.RowDefinitions>
<ColumnDefinition Width="25*" />
<ColumnDefinition Width="5*" />
<ColumnDefinition Width="25*" />
</Grid.RowDefinitions>
</Grid>
<Label
Grid.Column="0"
HorizontalTextAlignment="Center"
Text="{Binding Location}"
VerticalTextAlignment="Center" />
<Label
Grid.Column="1"
HorizontalTextAlignment="Center"
Text="{Binding TeacherName}"
VerticalTextAlignment="Center" />
<Label
Grid.Column="2"
HorizontalTextAlignment="Center"
Text="{Binding Time}"
VerticalTextAlignment="Center" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<!-- Center -->
<!-- Bottom -->
<Grid Grid.Row="2">
<Grid.RowDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="8*" />
<ColumnDefinition Width="1*" />
</Grid.RowDefinitions>
<!-- Useless stuff at the moment -->
</Grid>
<!-- Bottom -->
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</AbsoluteLayout>
</ContentPage.Content>
在CS部分,我有以下內容:
MainPage.xaml.cs:
public partial class MainPage : ContentPage
{
public ObservableCollection<Course> Courses { get; set; }
public MainPage()
{
base.BindingContext = this;
Debug.WriteLine("1.");
InitCoursesList();
Debug.WriteLine("2.");
InitializeComponent();
Debug.WriteLine("3.");
//CoursesListView.ItemsSource = Courses;
}
private void InitCoursesList()
{
Debug.WriteLine("1.1");
this.Courses = new ObservableCollection<Course>()
{
new Course()
{
Days = new List<Course.Day>() { Course.Day.M, Course.Day.W },
Title = "Object Oriented Programming",
Type = "CECS",
Number = "274",
Sections = new List<Section>()
{
new Section()
{
Location = "VEC-419",
TeacherName = "Foss S",
Time = "1-1:50pm"
},
new Section()
{
Location = "ECS-414",
TeacherName = "Foss S",
Time = "2-3:15pm"
}
}
},
new Course()
{
Days = new List<Course.Day>() { Course.Day.M, Course.Day.W },
Title = "Adv Artificial Intelligence",
Type = "CECS",
Number = "551",
Sections = new List<Section>()
{
new Section()
{
Location = "VEC-331",
TeacherName = "Todd Ebert",
Time = "3:30-4:45pm"
}
}
},
new Course()
{
Days = new List<Course.Day>() { Course.Day.Tu, Course.Day.Th },
Title = "Operating Syetems",
Type = "CECS",
Number = "326",
Sections = new List<Section>()
{
new Section()
{
Location = "VEC-330",
TeacherName = "Lam S",
Time = "9:30-10:20am"
},
new Section()
{
Location = "ECS-416",
TeacherName = "Lam S",
Time = "10:30-11:45am"
}
}
},
new Course()
{
Days = new List<Course.Day>() { Course.Day.Tu, Course.Day.Th },
Title = "C++ for Java Developers",
Type = "CECS",
Number = "282",
Sections = new List<Section>()
{
new Section()
{
Location = "VEC-518",
TeacherName = "Nachawati S",
Time = "11-11:50am"
},
new Section()
{
Location = "ECS-403",
TeacherName = "Nachawati S",
Time = "12-1:15pm"
}
}
},
};
Debug.WriteLine("1.2");
}
}
一切都很好,我的意思是顯示Debug.WriteLine();
。 我現在不了解的事情是為什么列表視圖為空? 對我來說,這沒有道理,但每個人都知道,問題通常出在電腦和椅子之間,所以..我在哪里錯了?
我將我的主ListView
綁定為<ListView x:Name="CoursesListView" ...>
與我的public ObservableCollection<Course> Courses { get; set; }
public ObservableCollection<Course> Courses { get; set; }
public ObservableCollection<Course> Courses { get; set; }
。 我填充它,初始化所有內容,但設置了綁定上下文,但最后。.頁面為空,為什么?
我也在問自己另一個問題。 如果我將集合綁定到列表,是否可以在此列表視圖的每個元素內綁定另一個列表? 如果您感到困惑,請查看每個Course
對象中的List<Section> Sections
。 在xaml部分,每個元素的中心部分都有一個列表。
我希望我的問題很清楚,也許這是一個愚蠢的問題,也許這個問題很有趣,我不知道,但是可以肯定的是,我沒有,我仍然不明白我的想法。
謝謝您幫忙。
您正在InitCoursesList函數中創建ObservableCollection
。 當您創建ObservableCollection
時,該綁定將已經與Courses綁定,其值將為null,因為您沒有引發PropertyChanged
事件(請參閱INotifyPropertyChanged
),所以不會更新該綁定,因此對該集合的任何添加都不會注冊。 解決方案是在創建包含類時創建集合:
public ObservableCollection<Course> Courses { get; set; } = new ObservableCollection<Course>();
然后在初始化函數中填充它:
private void InitCoursesList()
{
Courses.Add(new Course { etc });
}
當然,如果您想綁定第二個列表( ObservableCollection
),則可以這樣做。 在您的Course
類中,您僅具有一個ObservableCollection
並將其綁定到您想要用來查看該數據的任何控件。
如果將Courses屬性更改為依賴項屬性,則代碼將起作用。
public ObservableCollection<Course> Courses
{
get { return (ObservableCollection<Course>)GetValue(CoursesProperty); }
set { SetValue(CoursesProperty, value); }
}
public static readonly DependencyProperty CoursesProperty =
DependencyProperty.Register(nameof(Courses), typeof(ObservableCollection<Course>), typeof(MainPage));
正如已經在一個答案中所述,其原因是您正在重新創建ObservableCollection並中斷與舊對象的綁定,因為在重新分配它時不會觸發PropertyChanged事件。 由於這是一個UIElement,因此最好將其設置為DependencyProperty而不是實現INotifyPropertyChanged,您也可以選擇這樣做。 然后,您需要在Courses屬性的設置器中寫出完整的Courses屬性調用PropertyChanged。
此外,您還需要更新綁定語句...它需要查看MainPage而不是DataContext。
ItemsSource="{Binding Courses, Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:MainPage}}}
或命名您的MainPage x:Name =“ mainPage”並使用此綁定...
ItemsSource="{Binding Courses, ElementName=mainPage}"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.