【问题标题】:Issue using observable collection and a listbox - Wpf使用可观察集合和列表框的问题 - Wpf
【发布时间】:2014-02-23 14:02:28
【问题描述】:

我在使用 Observablecollection 和带有项目模板的列表框时遇到问题。

问题是,当我将它添加到可观察集合时,它似乎没有将它添加到列表框中。问题可能是数据上下文?还在学习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;
            }
        }
    }
}

主窗口 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));
            }
        }
    }
}

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    DataContext 设置为自身:

    DataContext = this;
    

    您也可以在 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}">
    

    【讨论】:

    • 感谢您的帮助。两者的表现完全一样吗?感谢您的更正,已修复。
    • 是的,你可以任意设置。
    猜你喜欢
    • 2014-01-22
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 2017-04-12
    相关资源
    最近更新 更多