2014-02-23 38 views
0

我無法使用Observablecollection和帶有項目模板的列表框。使用可觀察集合和列表框的問題 - Wpf

問題是,它似乎並沒有將它添加到列表框中,當我將它添加到可觀察集合。問題可能是數據上下文?仍在學習C#,所以它可能是一個newb錯誤,感謝您的幫助。

主窗口:

public partial class MainWindow : Window 
    { 

     public MainWindow() 
     { 
      InitializeComponent(); 

      DataContext = ? 

      Reminders.Add(new Remind(new DateTime(), "Hello")); 
      Reminders.Add(new Remind(new DateTime(), "asd")); 
      Reminders.Add(new Remind(new DateTime(), "gfs")); 
     } 

     private ObservableCollection<Remind> Reminders = new ObservableCollection<Remind>(); 

     public ObservableCollection<Remind> reminders 
     { 
      get 
      { 
       return Reminders; 
      } 
     } 
    } 
} 

主窗口的XAML

<Window x:Class="Reminder.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="Reminder" Height="357" Width="372"> 
    <Grid Margin="0,0,2,2"> 
     <Menu HorizontalAlignment="Left" Height="26" VerticalAlignment="Top" Width="507"> 
      <MenuItem Header = "File" Width="32"> 
       <MenuItem Header="New Reminder"/> 
       <MenuItem Header="Delete Reminder"/> 
       <MenuItem Header="Change"/> 
      </MenuItem> 
      <MenuItem Header="Options"> 

      </MenuItem> 
      <MenuItem Header="About"> 
       <MenuItem Header="Info"/> 
      </MenuItem> 
     </Menu> 
     <Button Content="New" HorizontalAlignment="Left" Height="26" Margin="6,279,0,0" VerticalAlignment="Top" Width="81" /> 
     <Button Content ="Delete" HorizontalAlignment="Left" Height="26" Margin="87,279,0,0" VerticalAlignment="Top" Width="79" /> 
     <Button Content="Change" HorizontalAlignment="Left" Height="26" Margin="166,279,0,0" VerticalAlignment="Top" Width="73" /> 
     <ScrollViewer Name="Scroller" HorizontalAlignment="Left" Height="235" Margin="0,31,0,0" VerticalAlignment="Top" Width="346"> 
      <ListBox ItemsSource= "{Binding reminders}"> 
       <ListBox.ItemTemplate> 
        <DataTemplate> 
         <StackPanel Height="41" Width="293" > 
          <TextBlock Text="{Binding Path=dateT}"/> 
          <TextBlock Text="{Binding Path=Msg}"/> 
         </StackPanel> 
        </DataTemplate> 
       </ListBox.ItemTemplate> 

      </ListBox> 
     </ScrollViewer> 
     <Separator HorizontalAlignment="Left" Height="13" Margin="0,266,0,0" VerticalAlignment="Top" Width="362"/> 


    </Grid> 
</Window> 

提醒代碼:

namespace Reminder 
{ 
    public class Remind : INotifyPropertyChanged 
    { 


     public Remind(DateTime dt, string ms) 
     { 
      dateT = dt; 
      Msg = ms; 
     } 

     private DateTime datet; 

     public DateTime dateT 
     { 
      get 
      { 
       return datet; 
      } 

      set 
      { 
       if (datet != value) 
       { 
        datet = value; 
        RaisePropertyChange("dateT"); 
       } 
      } 
     } 

     private string msg; 

     public string Msg 
     { 
      get 
      { 
       return msg; 
      } 
      set 
      { 
       if (msg != value) 
       { 
        msg = value; 
        RaisePropertyChange("Msg"); 
       } 
      } 
     } 


     public event PropertyChangedEventHandler PropertyChanged; 

     protected void RaisePropertyChange(string name) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(name)); 
      } 
     } 
    } 
} 

回答

1

設置DataContext本身:

DataContext = this; 

OR

可以在XAML設置,以及:

<Window DataContext="{Binding RelativeSource={RelativeSource Self}}"> 

在一個側面說明屬性名應當在Pascal大小寫和私人領域的備份應該有駱駝外殼。所以,按照慣例應該是:

private ObservableCollection<Remind> reminders = 
        new ObservableCollection<Remind>(); 

public ObservableCollection<Remind> Reminders 
{ 
    get 
    { 
     return reminders; 
    } 
} 

和需要更新XAML以及結合:

<ListBox ItemsSource= "{Binding Reminders}"> 
+0

感謝您的幫助。這兩個表演完全一樣嗎?感謝您糾正它,修復它。 – user2469515

+0

是的,你可以任意設置它。 –