使用可观察集合和列表框的问题 - Wpf

时间:2014-02-23 05:53:16

标签: c# wpf xaml

我无法使用Observablecollection和带有项目模板的列表框。

问题是,当我将它添加到observable集合时,似乎没有将它添加到列表框中。问题可能是数据上下文?还在学习C#,所以这可能是一个新的错误,谢谢你的帮助。

主窗口:

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

Mainwindow 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 个答案:

答案 0 :(得分:1)

DataContext设置为自身:

DataContext = this;

OR

您也可以在XAML中设置:

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

旁注属性名称应该在Pascal Case中,私有备份字段应该有驼峰。因此,按照惯例,它应该是:

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

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

并且还需要更新XAML中的绑定:

<ListBox ItemsSource= "{Binding Reminders}">