如何使用 INotifyPropertyChanged 更新列表框
我有一个日历,并且在日历项目控件中嵌套了一个项目控件,以便我可以在我想要为其创建事件的网格单元(日期)中显示事件。压延机为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"));
}
这是当程序启动并运行后我想添加事件时弹出的窗口(类)。我需要弄清楚如何使用此窗口添加项目以及如何将其添加到特定日期(gridcell)
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; }
}
}
}
I have a calender and I nested a itemscontrol within the calendars itemscontrol so that I can display events in the gridcell(date) that I want to make an event for. The calender is 7 columns and 6 rows. Right now I'm just using a listbox to actually display the event(Titles). I can add items on startup but I want to be able to add/remove/change once the program is already running. I have tried INotifyPropertyChanged once before and I could not figure it out for the life of me. If someone can take what i have and show me or give me tips on how I can do this, I would really appreciate it.
The class where I can add(items) before startup:
public class eventProperties : ObservableCollection<eventsTitles>
{
//public string Eventer { get; set; }
public eventProperties() : base()
{
Add(new eventsTitles("First Test"));
Add(new eventsTitles("First Test#2"));
}
This is window(class) that pops up when I want to add an event once the program is up and running already. I need to figure out how to add items from using this window and how to add it to a certain date(gridcell)
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" });
This is the actual calender XAML where I need to bind everything.
</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 Class
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; }
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Harsh 的答案对于您实施
INotifyPropertyChanged 的方式是正确的
,但是您只需要为单个属性实现它。如果您希望通知 UI Collection 已更改,请使用ObservableCollection
类。因此,如果您有一个包含
Event.EventTitle
的 Label,并且您想要更改在代码隐藏中的EventTitle
中,您可以在EventClass
上实现INotifyPropertyChanged
,以便在更新myEvent.EventTitle
时UI 中的标签将自动更新为新值。如果您有一个 ListBox(或 ItemsControl),并且希望在向集合添加项目或从集合中删除项目时自动更新,只需将
ListBox
绑定到ObservableCollection
属性即可,当添加或删除项目时,它将自动更新 UI。例如,
在此示例中,您的
Schedule
类将具有一个名为Events
的属性,它是一个ObservableCollection
。要添加新事件,您可以使用如下内容:作为旁注,您应该查看一些 .Net 命名约定
Harsh's answer is correct for how you implement
INotifyPropertyChanged
, however you only need to implement that for single properties. If you're looking to notify the UI that a Collection has changed, use theObservableCollection
classSo if you had a Label containing
Event.EventTitle
, and you wanted to change theEventTitle
in the code-behind, you would implementINotifyPropertyChanged
on theEventClass
so that when you updatedmyEvent.EventTitle
the Label in the UI would automatically update to the new value.If you have a ListBox (or ItemsControl) which you want to automatically update when you add or delete items to/from a Collection, simply bind the
ListBox
to anObservableCollection
property, which will automatically update the UI when items get added or removed from it.For example,
In this example, your
Schedule
class would have a property calledEvents
which is anObservableCollection<Event>
. To add a new event you can use something like this:As a side note, you should check out some .Net naming conventions
我认为
eventTitles
一定存在未实现INotifyPropertyChanged
接口的问题,因此这里是实现的详细信息。假设 eventTitles 类
如需进一步参考,您可以查看此 MSDN发布。
I am thinking that
eventTitles
must be having some issue of not implementingINotifyPropertyChanged
interface so here is the details for implementing.Supposing eventTitles class
For further reference you could check at this MSDN Post.