如何使用INotifyPropertyChanged更新ListBox

本文关键字:ListBox 更新 INotifyPropertyChanged 何使用 | 更新日期: 2023-09-27 18:13:52

我有一个日历,我在日历的itemscontrol中嵌套了一个itemscontrol,这样我就可以在gridcell(date)中显示我想要为其创建事件的事件。日历是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; }
            }
       }        
  }

如何使用INotifyPropertyChanged更新ListBox

Harsh的答案对于如何实现INotifyPropertyChanged是正确的,但是您只需要为单个属性实现它。如果您希望通知UI集合已经更改,请使用ObservableCollection

所以如果你有一个包含Event.EventTitle的标签,你想在代码后面改变EventTitle,你可以在EventClass上实现INotifyPropertyChanged,这样当你更新myEvent.EventTitle时,UI中的标签会自动更新到新的值。

如果你有一个ListBox(或ItemsControl),你想在添加或删除项目时自动更新,只需将ListBox绑定到ObservableCollection属性,当项目被添加或删除时,它将自动更新UI

例如,

<ItemsControl.ItemTemplate>
    <DataTemplate>
        <StackPanel>
         <Button Content="{Binding day}" Click="AddEvent" />
         <ItemsControl Height="60" ItemsSource="{Binding Events}">
             <ItemsControl.ItemTemplate>
                 <DataTemplate>
                    <TextBlock Text="{Binding EventTitle}" />
                 <DataTemplate>
             </ItemsControl.ItemTemplate>
         </ItemsControl>
        </StackPanel>
    </DataTemplate>
</ItemsControl.ItemTemplate>

在这个例子中,你的Schedule类将有一个名为Events的属性,它是ObservableCollection<Event>。要添加一个新事件,您可以使用如下命令:

void AddEvent(object sender, EventArgs e)
{
    // Get the DataContext for the Button, which should be of type Schedule
    Schedule day = (sender as Button).DataContext as Schedule;
    // Add New Event. Since this is an ObservableCollection, UI will automatically update
    day.Events.Add(new Event { EventTitle = "Test Event" });
}

作为旁注,您应该查看一些。net命名约定

我认为eventTitles必须有一些没有实现INotifyPropertyChanged接口的问题,所以这里是实现的细节。

假设eventTitles类
public class eventTitles : INotifyPropertyChanged
{
    private string _eventtitle = string.Empty;
    public string EventTitle
    {
       get {return _eventtitle;}
        set
        {
            if (value != _eventtitle)
            {
                _eventtitle = value;
                NotifyPropertyChanged("EventTitle");
            }
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}