ObservableCollection 未更新
我觉得我已经很接近让它发挥作用了。我有一个日历,我希望用户能够从组合框中选择一个月并显示该日期的日历。现在,无论我从组合框中选择什么,都不会显示任何内容。我可以通过使用列表框和按钮事件来使其工作,但它并没有真正更新它只是清除并显示选择的新月份。这不是正确的做法。我已经投入了很多时间,如果有人可以看一下并给我一些指示,我将非常感激。
--------模型类---------
public partial class SchedulePage : Page, INotifyPropertyChanged
{
public int pick2;
public event PropertyChangedEventHandler PropertyChanged;
MainWindow _parentForm;
public int pick;
Schedule sched = new Schedule();
static GregorianCalendar _gc = new GregorianCalendar();
public SchedulePage(MainWindow parentForm)
{
InitializeComponent();
// this.PropertyChanged += comboMonth_SelectionChanged;
pick = Convert.ToInt32(comboMonth.SelectedItem);
_parentForm = parentForm;
}
public void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private int _nameofmonth ;
public int NameofMonth
{
get
{
return this._nameofmonth;
}
set
{
if (value != this._nameofmonth)
{
this._nameofmonth = value;
NotifyPropertyChanged("NameofMonth");
}
}
}
// void TheViewModel_PropertyChanged(object src, PropertyChangedEventArgs e)
private void comboMonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//{
// if (e.PropertyName == "NameofMonth")
// {
//var date = new DateTime(2011, 11, 1);
//makeCalender(date);
_parentForm.bindings.schedule.Clear();
var t = new List<Schedule>();
DateTime curr = DateTime.Now;
int jeez = comboMonth.SelectedIndex+1;
// comboMonth.Items.Add(curr.Month);
DateTime newcurr = new DateTime(2011, NameofMonth+1, 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 == NameofMonth+1; 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;
comboMonth.DataContext = _parentForm.bindings;
DataContext = _parentForm.bindings;
// }
}
----XAML的一部分-----
<ComboBox SelectedIndex="{Binding NameofMonth}" Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Left" Margin="6,0,0,0" Name="comboMonth" VerticalAlignment="Top" Width="120" SelectionChanged="comboMonth_SelectionChanged">
<ComboBoxItem Content="1" 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>
I feel like i'm really close to getting this to work. I have a calender, I want the user to be able to pick a month from a combobox and show the calender for that date. Right now nothing is being displayed no matter what I choose from the combobox. I can get it to work by using a listbox and a button event but its not really updating its just clearing and displaying the new month choosen. It's not the right way to do it. I've put a lot of time into this and if anybody can look at and maybe give me some pointers i'd really appreciate it.
--------Model Class---------
public partial class SchedulePage : Page, INotifyPropertyChanged
{
public int pick2;
public event PropertyChangedEventHandler PropertyChanged;
MainWindow _parentForm;
public int pick;
Schedule sched = new Schedule();
static GregorianCalendar _gc = new GregorianCalendar();
public SchedulePage(MainWindow parentForm)
{
InitializeComponent();
// this.PropertyChanged += comboMonth_SelectionChanged;
pick = Convert.ToInt32(comboMonth.SelectedItem);
_parentForm = parentForm;
}
public void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
private int _nameofmonth ;
public int NameofMonth
{
get
{
return this._nameofmonth;
}
set
{
if (value != this._nameofmonth)
{
this._nameofmonth = value;
NotifyPropertyChanged("NameofMonth");
}
}
}
// void TheViewModel_PropertyChanged(object src, PropertyChangedEventArgs e)
private void comboMonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//{
// if (e.PropertyName == "NameofMonth")
// {
//var date = new DateTime(2011, 11, 1);
//makeCalender(date);
_parentForm.bindings.schedule.Clear();
var t = new List<Schedule>();
DateTime curr = DateTime.Now;
int jeez = comboMonth.SelectedIndex+1;
// comboMonth.Items.Add(curr.Month);
DateTime newcurr = new DateTime(2011, NameofMonth+1, 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 == NameofMonth+1; 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;
comboMonth.DataContext = _parentForm.bindings;
DataContext = _parentForm.bindings;
// }
}
----Part of the XAML------
<ComboBox SelectedIndex="{Binding NameofMonth}" Grid.ColumnSpan="2" Height="23" HorizontalAlignment="Left" Margin="6,0,0,0" Name="comboMonth" VerticalAlignment="Top" Width="120" SelectionChanged="comboMonth_SelectionChanged">
<ComboBoxItem Content="1" 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使 ObservableCollection 中使用的 poco 类实现 INotifyChanged。
例子:
You need to make your poco class that is used within your ObservableCollection implement INotifyChanged.
Example: