【发布时间】:2014-01-06 18:15:51
【问题描述】:
我正在使用 C#/XAML 以及 MVVM-Light Toolkit 编写 Windows 8.1 应用程序。
在我的程序中有一个由 3 个组件组成的时间表:
- 一个包含 5 个元素的 GridView,用于星期一、星期二……
- 带有 x 个元素的 ListView,每个元素显示 当前期间。 x 取决于用户选择的周期数 让他的日程安排。
- 带有 5*x 个元素的 GridView,表示事件的位置 由用户设置。
这 3 个组件再次位于 FlipView 中以启用多个计划。 我通过以下对象在代码中启用了此功能:
public class Schedule
{
public int WeekNumber { get; set; }
public ScheduleComponentSettings ScheduleComponentSettings { get; set; }
public ScheduleComponents ScheduleComponents { get; set; }
}
public class ScheduleComponents
{
public ObservableCollection<WeekDay> WeekDayItems { get; set; }
public ObservableCollection<FreePeriod> FreePeriodItems { get; set; }
public ObservableCollection<PeriodTime> PeriodTimesItems { get; set; }
public ObservableCollection<LessonTime> LessonTimesItems { get; set; }
}
在我的 ViewModel 中,我有一个 Schedule 类的 ObservableCollection:
public ObservableCollection<Schedule> ScheduleComponentsList
{
get
{
return _ScheduleComponentsList;
}
set
{
if (_ScheduleComponentsList == value)
{
return;
}
RaisePropertyChanging(ScheduleWeekListPropertyName);
_ScheduleComponentsList = value;
RaisePropertyChanged(ScheduleWeekListPropertyName);
}
}
FlipView 及其元素按如下方式绑定到它(这是 ofc。缩短为仅显示 ItemsSources):
<FlipView
ItemsSource="{Binding Main.ScheduleComponentsList, Source={StaticResource Locator}}"
<FlipView.ItemTemplate>
<DataTemplate>
<GridView
ItemsSource="{Binding ScheduleComponents.WeekDayItems}"/>
<ListView
ItemsSource="{Binding ScheduleComponents.PeriodTimesItems}"/>
<GridView
ItemsSource="{Binding ScheduleComponents.FreePeriodItems}"/>
</DataTemplate>
</FlipView.ItemTemplate>
</FlipView>
现在问题来了: 当我更改 FreePeriodItems 内的元素的属性时,甚至当我完全替换集合时,视图只会在我重新加载整个页面时更新。对于我在 ScheduleComponents 上更新的所有其他属性也是如此。 但是,当我更改 ScheduleComponentList 本身时,不会发生这种情况。例如,当我向其中添加项目时,它们会在视图中自动更新。
现在我已经坐了很久了。
【问题讨论】:
-
我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
标签: c# xaml data-binding mvvm windows-8.1