2011-11-09 61 views
2

我有一個日曆,我在日曆itemscontrol中嵌套了一個itemscontrol,以便我可以在gridcell(日期)中顯示要爲其生成事件的事件。壓光機是7列6行。現在我只是使用列表框來實際顯示事件(標題)。我可以在啓動時添加項目,但我希望能夠在程序已經運行時添加/刪除/更改。我曾嘗試過一次INotifyPropertyChanged,但我無法弄清楚它是否適合我的生活。如果某人可以拿走我擁有的東西並給我看,或者給我提示我如何做到這一點,我會非常感激。如何使用INotifyPropertyChanged更新ListBox

類,我可以啓動前添加(項):

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; } 
      } 
     }   
    } 
+0

你能告訴我們eventsTitles'類'的實施,這部分是小鬼的回答正確... –

+0

肯定可以一秒。 – TMan

+0

檢查答案。 –

回答

2

Harsh's answer對於您如何實現INotifyPropertyChanged是正確的,但是您只需要爲單個屬性實現該功能。如果你正在尋找以通知集合改變了UI,使用ObservableCollection

所以,如果你有包含Event.EventTitle一個標籤,你想改變EventTitle在後臺代碼,你將實施INotifyPropertyChangedEventClass,以便當您更新myEvent.EventTitle時,UI中的標籤將自動更新爲新值。

如果你有一個列表框(或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 naming conventions

+0

我收到錯誤的按鈕說沒有名稱空間或定義找到。 (發件人爲「按鈕」)。任何想法 – TMan

+0

@TMan你需要導入命名空間'System.Windows.Controls' – Rachel

+0

好吧,我確實有一個快速問題。我有一個schedule.cs,只是ObservableCollection的屬性爲,然後我有一個schedulePage.xaml.cs,這是顯示日曆的頁面,以及我將所有xaml信息放在哪裏。但schedulePage.xaml.cs中的第一個itemscontrol(calender)綁定到schedule.cs,所以你告訴我的所有東西都應該放在schedule.cs或schedulePage.xaml.cs中,我試過了schedulePage,當我點擊按鈕時它會顯示一個窗口必須是樹的根。另外我的事件類也是一個解釋錯誤的窗口,我應該做一個單獨的event.cs嗎? – TMan

1

我想到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)); 
     } 
    } 
} 

有關進一步的參考,你可以檢查這個MSDN Post