【发布时间】:2011-11-09 13:34:14
【问题描述】:
我有一个日历,并且我在日历项控件中嵌套了一个项控件,以便我可以在网格单元(日期)中显示我想要为其创建事件的事件。日历为 7 列和 6 行。现在我只是使用一个列表框来实际显示事件(标题)。我可以在启动时添加项目,但我希望能够在程序已经运行后添加/删除/更改。我以前尝试过 INotifyPropertyChanged 一次,但我一生都无法弄清楚。如果有人可以拿走我所拥有的东西并给我看或给我一些关于如何做到这一点的提示,我将非常感激。
我可以在启动前添加(项目)的类:
public class eventProperties : ObservableCollection<eventsTitles>
{
//public string Eventer { get; set; }
public eventProperties() : base()
{
Add(new eventsTitles("First Test"));
Add(new eventsTitles("First Test#2"));
}
这是一个窗口(类),当我想在程序启动并运行后添加一个事件时弹出。我需要弄清楚如何使用此窗口添加项目以及如何将其添加到某个日期(网格单元)
public Event(MainWindow parentform)
{
InitializeComponent();
_parentForm = parentform;
//this.month = month;
//this.day = day;
//this.year = year;
lblCreateEvent.Content = "Create Event For " + month + " / " + day + " / " + year;
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
month = Convert.ToInt32(txtMonth.Text);
day = Convert.ToInt32(txtDay.Text);
year = Convert.ToInt32(txtYear.Text);
Schedule sched = new Schedule();
DateTime curr = DateTime.Now;
int[] m = new int[7];
DateTime newcurr = new DateTime(year, month, day);
var cal = System.Globalization.DateTimeFormatInfo.CurrentInfo.Calendar;
var ms = cal.GetWeekOfYear(new DateTime(newcurr.Year, newcurr.Month, 1), System.Globalization.CalendarWeekRule.FirstDay, System.DayOfWeek.Sunday);
// for (var i = 1; newcurr.Month == 11; newcurr = newcurr.AddDays(1))
// {
var month_week = (newcurr.Day / 7);
sched.MonthWeek = newcurr.GetWeekOfMonth().ToString();
sched.Month = newcurr.Month.ToString();
sched.Year = newcurr.Year.ToString();
sched.day = newcurr.Day.ToString();
sched.WeekOfYear = cal.GetWeekOfYear(newcurr, System.Globalization.CalendarWeekRule.FirstDay, DayOfWeek.Sunday).ToString();
sched.dayofweek = newcurr.DayOfWeek.ToString();
//Here is where the calender is created. By looping through all the days in the selected month it will find the weeknumber and day of the week -->
// that that particular date belongs to and place in the correct gridcell.
_parentForm.bindings.schedule.Add(new Schedule { WeekNo = newcurr.GetWeekOfMonth() - 1, WeekDay = (int)newcurr.DayOfWeek, day = newcurr.Day.ToString(), eventTitle = "Camper Calender" });
这是我需要绑定所有内容的实际日历 XAML。
</customgridcontrol:GridControl>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<Button Content="{Binding day}" Width="175" HorizontalAlignment="Stretch" VerticalAlignment="Top" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Name="btnCalenderDate" Click="btnCalenderDate_Click" Loaded="btnCalenderDate_Loaded" Height="18" FontSize="10" FontWeight="Bold">
</Button>
<ItemsControl Height="60" VerticalAlignment="Stretch">
<ItemsControl.Resources>
<src:eventProperties x:Key="dataList"/>
</ItemsControl.Resources>
<ItemsControl.Items>
<ListBox ItemsSource="{Binding Source= {StaticResource dataList} }" DisplayMemberPath="EventTitle" VerticalAlignment="Top" IsSynchronizedWithCurrentItem="True">
</ListBox>
</ItemsControl.Items>
</ItemsControl>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<!-- ItemContainerStyle -->
<ItemsControl.ItemContainerStyle>
<Style >
<Setter Property="Grid.Column" Value="{Binding WeekDay}" />
<Setter Property="Grid.Row" Value="{Binding WeekNo}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
eventTitles 类
namespace Camp_
{
public class eventsTitles
{
public string eventTitle {get; set;}
public eventsTitles()//String ev)
{
// this.eventTitle = ev;
}
public string EventTitle
{
get { return eventTitle; }
set { eventTitle = value; }
}
}
}
【问题讨论】:
-
你能告诉我们
eventsTitles类的实现吗,那部分是为了正确回答... -
为什么要创建一个继承自
ObservableCollection的类?为什么不在day类上添加一个ObservableCollection<eventTitle>类型的属性? -
@Rachel 我在这里看到了很多,但一直不明白人们为什么这样做。我只能假设在示例中某处有一个教程可以做到这一点
-
有,我从 msdn 获得了示例。 msdn.microsoft.com/en-us/library/…
标签: c# wpf visual-studio inotifypropertychanged