【发布时间】:2012-06-13 17:43:13
【问题描述】:
这个场景我很好,我让模型解释一下。
public class ScheduleMonthlyPerDayModel
{
public DateTime Date { get; set; }
public string Day
{
get
{
return Date.Day.ToString();
}
}
ObservableCollection<AppointmentDTO> _appointments;
public ObservableCollection<AppointmentDTO> Appointments
{
get
{
return _appointments;
}
set
{
_appointments = value;
if (value.Count > 0)
NotifyOfPropertyChange(() => HasSchedule);
}
}
public bool BelongsToCurrentMonth
{
get;
set;
}
public bool HasSchedule
{
get
{
return _appointments.Count > 0 ? true : false;
}
}
public ScheduleMonthlyPerDayModel()
{
_appointments = new ObservableCollection<AppointmentDTO>();
}
public void ClearCollection()
{
_appointments.Clear();
}
}
public class ScheduleMonthlyPerWeekModel
{
public ScheduleMonthlyPerDayModel Sunday{get; set;}
public ScheduleMonthlyPerDayModel Monday{get; set;}
public ScheduleMonthlyPerDayModel Tuesday{get; set;}
public ScheduleMonthlyPerDayModel Wednesday{get; set;}
public ScheduleMonthlyPerDayModel Thursday{get; set;}
public ScheduleMonthlyPerDayModel Friday{get; set;}
public ScheduleMonthlyPerDayModel Saturday{get; set;}
}
与 xaml 的绑定正在使用 xaml 的一瞥,如下所示:
headereditemscontrol itemsSource= weekcollection,其中 weekcollection 是 schedulemonthlyperweekmodel 的对象。
在该 headereditems 控件中,我每天为 schedulemonthlyperweekmodel 的每个属性模板化如下:
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Style="{StaticResource CalendarDates}" Text="{Binding Path=Saturday.Day}" />
<ListBox Grid.Row="1" Grid.ColumnSpan="2" Padding="0"
ItemsSource="{Binding Path= Saturday.Appointments}"
ItemTemplate="{StaticResource myItemStyle}"
Visibility="{Binding Path=Saturday.HasSchedule, Converter={StaticResource BoolToVisibilityConverter}}" />
基本上,我试图通过每天收集约会来实现每月视图。我的问题是,当我以编程方式将项目添加到例如此处的 saturday.appointments 集合时,通过调试附加项目成功并通知主集合(weekcollection),不会刷新 UI。
我想要实现的是:在我将假定的约会添加到其相应的日期/日期后,用户界面也会相应地更新,但我该怎么做呢?
目前,UI 仅在我更改/切换到不同然后返回时才会更新,之后会很好地显示约会。我想自动化它,因为要求用户在看到约会列表之前切换到其他东西然后返回是很丑陋的。
【问题讨论】:
-
NotifyOfPropertyChange是什么,我没有看到 INotifyPropertyChanged 已实现。 -
如果你正在绑定控件的数据源,你不应该“刷新”任何东西。
-
现在,如果您将一个项目添加到 Appointments 集合中,它应该会自动在 ListBox 中显示该项目。但是,当列表从空变为填充时,可见性绑定不会更新。这是真的吗?
-
是的@Moozhe,这就是为什么我想我需要将当前视图切换到其他视图然后返回以使列表框可见。那么我应该如何进行呢?省略可见性代码在性能方面会更好吗?我尝试关闭视图然后以编程方式返回,但这很丑陋,我希望有更好的行动方案。 @nickbabcock 它是由 schedulemonthlyperdaymodel 继承的,显然我不小心删除了继承。谢谢。
-
只是一个额外的,我可以离开列表框的可见性,它会工作并显示约会,但我也有 datatriggers 设置为 HasSchedule 属性,当它有约会时会改变单元格背景,所以我认为这涉及相同的问题,如何将内部/辅助属性的更改反映到 xaml 以更新用户界面。
标签: c# wpf binding observablecollection