Это убивает меня.Прямо сейчас, создав новый DateTime
, я могу отображать только один месяц в календаре (сетке), который у меня есть.Например, если я скажу DateTime(2011,10,1)
, он найдет weekofmonth и dayofweek и разместит каждую дату на правом квадрате в календаре (сетка, 7 столбцов, 6 строк).
Я хочу иметь возможностькаким-то образом выбрать из списка или списка, или что-то еще, и выбрать месяц, и календарь будет обновляться до этого месяца и года.У меня есть коллекция INotifyPropertyChanged
и Observable
. Я связываю grid.columns с днём недели, а grid.row с номером недели (именно так он находит место для размещения каждой даты).Я на самом деле не знаю, чтобы обновить и показать еще один месяц.Я читал кое-что о создании коллекции, но я не уверен, как это будет сделано и как это работает с INotifyPropertyChanged
.
Я потратил на это гораздо больше времени, чем следовало бы.Пожалуйста, любая помощь приветствуется.
Расписание класса:
public class Schedule : INotifyPropertyChanged
{
private int MonthofYear = 11;
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public int NameofMonth
{
get
{
return this.MonthofYear;
}
set
{
if (value != this.MonthofYear)
{
this.MonthofYear = value;
NotifyPropertyChanged("NameofMonth");
}
}
}
// public void UpdateCal(PropertyChangedEventArgs e)
// {
// if (PropertyChanged != null)
// PropertyChanged(this, e);
// }
public string MonthWeek { get; set; }
public string Year { get; set; }
public string Month { get; set; }
public string day { get; set; }
public string WeekOfYear { get; set; }
public string dayofweek { get; set; }
public int WeekNo { get; set; }
public int WeekDay { get; set; }
public DateTime Date { get; set; }
Этот класс делает ObservableCollection
:
public partial class BindingCamper : Schedule
{ // This class assist in binding campers from listview to the textboxes on the camperspage
public ObservableCollection<Camper> Campers { get; set; }
public ObservableCollection<Staff> StaffMembers { get; set; }
public ObservableCollection<Schedule> schedule { get; set; }
Schedule hut = new Schedule();
public BindingCamper()
{
Campers = new ObservableCollection<Camper>();
StaffMembers = new ObservableCollection<Staff>();
schedule = new ObservableCollection<Schedule>();
//NotifyPropertyChanged("schedule");
}
Это важный класс.DateTime newcurr = DateTime(2011, 10, 1)
- это все, что я могу вставить. Это создает календарь на эту дату.Я хочу иметь возможность создавать календари для нескольких дат (хотя и по одному).
static class DateTimeExtensions
{
static GregorianCalendar _gc = new GregorianCalendar();
public static int GetWeekOfMonth(this DateTime time)
{
DateTime first = new DateTime(time.Year, time.Month, 1);
return time.GetWeekOfYear() - first.GetWeekOfYear() + 1;
}
static int GetWeekOfYear(this DateTime time)
{
return _gc.GetWeekOfYear(time, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
}
}
public partial class SchedulePage : Page
{
public int pick2;
MainWindow _parentForm;
public int pick;
Schedule sched = new Schedule();
private ObservableCollection<Schedule> schedules;
public ObservableCollection<Schedule> Sched
{
get { return schedules; }
set
{
if (Equals(schedules, value)) return;
schedules = value;
sched.NotifyPropertyChanged("Sched");
}
}
public SchedulePage(MainWindow parentForm)
{
InitializeComponent();
sched.NameofMonth = comboMonth.SelectedIndex;
pick = Convert.ToInt32(comboMonth.SelectedItem);
_parentForm = parentForm;
// DateTime date = new DateTime(year, month, day);
int ugly = sched.NameofMonth;
AcceptDate(2011, 12);
}
public void AcceptDate(int year, int acceptmonth) {
var t = new List<Schedule>();
DateTime curr = DateTime.Now;
int y = acceptmonth;
// comboMonth.Items.Add(curr.Month);
DateTime newcurr = new DateTime(2011, 12, 1);
// pickdate = datePickercal.SelectedDate;
// DateTime newcurr = new DateTime(curr.Year, curr.Month, 1);
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 == acceptmonth; 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();
t.Add(sched);
_parentForm.bindings.schedule.Add(new Schedule { WeekNo = newcurr.GetWeekOfMonth() - 1, WeekDay = (int)newcurr.DayOfWeek, day = newcurr.Day.ToString() });
}
lblDate.Content = (newcurr.Month - 1) + "/" + newcurr.Year;
//testGrid.ItemsSource = t;
DataContext = _parentForm.bindings;
}
private void btnDateRight_Click(object sender, RoutedEventArgs e)
{
}
private void btnDateLeft_Click(object sender, RoutedEventArgs e)
{
}
}
И для XAML Itemsource = schedule
<Grid>
<ItemsControl ItemsSource="{Binding schedule}" Name="Calender" Margin="0,49,0,0" VerticalAlignment="Stretch">
<ItemsControl.Template>
<ControlTemplate TargetType="ItemsControl" >
<Border BorderBrush="Aqua" BorderThickness="1" CornerRadius="15">
<ItemsPresenter/>
</Border>
</ControlTemplate>
</ItemsControl.Template>
<!-- ItemsPanelTemplate -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid ShowGridLines="True" Name="gridCalender">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock OpacityMask="Black" Name="txtBlockdays">
<Button Content="{Binding day}" Width="175" HorizontalAlignment="Stretch" VerticalAlignment="Top" VerticalContentAlignment="Top" HorizontalContentAlignment="Left" Background="#FF94EBEB">
</Button>
</TextBlock>
</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>
<Grid Margin="0,0,0,262">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Content="Sunday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,12,0" Name="label1" VerticalAlignment="Top" Width="Auto" />
<Label Content="Monday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,0,0" Name="label2" VerticalAlignment="Top" Grid.Column="1" />
<Label Content="Tuesday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,0,0" Name="label3" VerticalAlignment="Top" Grid.Column="2" />
<Label Content="Wednesday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,0,0" Name="label4" VerticalAlignment="Top" Grid.Column="3" />
<Label Content="Thursday" Height="28" HorizontalAlignment="Stretch" Name="label5" VerticalAlignment="Top" Grid.Column="4" Margin="0,25,0,0" />
<Label Content="Friday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,0,0" Name="label6" VerticalAlignment="Top" Grid.Column="5" />
<Label Content="Saturday" Height="28" HorizontalAlignment="Stretch" Margin="0,25,0,0" Name="label7" VerticalAlignment="Top" Grid.Column="6" />
<Label Height="28" HorizontalAlignment="Left" Margin="63,0,0,0" Name="lblDate" VerticalAlignment="Top" Grid.Column="2" Width="127" Grid.ColumnSpan="3" />
<Button Content="<" Height="23" HorizontalAlignment="Left" Margin="28,6,0,0" Name="btnDateLeft" VerticalAlignment="Top" Width="29" Grid.Column="2" Command="NavigationCommands.BrowseBack" />
<Button Content=">" Height="23" HorizontalAlignment="Left" Margin="20,5,0,0" Name="btnDateRight" VerticalAlignment="Top" Width="30" Grid.Column="4" Command="NavigationCommands.BrowseForward" />
<ComboBox Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Left" Margin="6,0,0,0" Name="comboMonth" VerticalAlignment="Top" Width="120">
<ComboBoxItem Content="{Binding Month}" IsSelected="False" />
<ComboBoxItem Content="2" />
<ComboBoxItem Content="3" />
<ComboBoxItem Content="4" />
<ComboBoxItem Content="5" />
<ComboBoxItem Content="6" />
<ComboBoxItem Content="7" />
<ComboBoxItem Content="8" />
<ComboBoxItem Content="9" />
<ComboBoxItem Content="10" />
<ComboBoxItem Content="11" IsSelected="False" />
<ComboBoxItem Content="12" />
</ComboBox>
<ListBox Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Left" Margin="48,0,0,0" Name="listMe" VerticalAlignment="Top" Width="120" Grid.Column="5">
<ListBoxItem Content="01" IsSelected="False" />
<ListBoxItem Content="3" IsSelected="True" />
<ListBoxItem Content="2" />
</ListBox>
</Grid>
</Grid>