简体   繁体   English

ObservableCollection无法通过INotifyPropertyChanged更新

[英]ObservableCollection not updating through INotifyPropertyChanged

I've been trying to get this to work but I still have no luck.. Basically I'm using an ObservableColletion to load all my Items into a Listbox which has a custom ItemTemplate. 我一直在尝试使其工作,但我仍然没有运气。基本上,我正在使用ObservableColletion将所有项目加载到具有自定义ItemTemplate的列表框中。

This is my XAML for the ListBox: 这是我对ListBox的XAML:

<DataTemplate x:Key="DataTemplate1">
        <Grid d:DesignWidth="431" d:DesignHeight="109.333" Height="109.333" Width="431">
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu IsFadeEnabled="True" IsZoomEnabled="False">
                    <toolkit:MenuItem Header="Delete" Click="Delete_Click"/>
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
            <Button x:Name="callPerson" Tag="{Binding ItemNumber}" Content="CALL" HorizontalAlignment="Left" Margin="-6,12,0,0" VerticalAlignment="Top" Click="callPerson_Click"/>
            <Grid toolkit:TiltEffect.IsTiltEnabled="True" Margin="0,-1,-121,-3">
                <TextBlock Text="{Binding ItemBody}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="26" Width="387"  FontSize="19" Margin="109,62,0,0" FontFamily="Segoe WP" TextTrimming="WordEllipsis">
                    <TextBlock.Foreground>
                        <SolidColorBrush Color="{StaticResource PhoneTextMidContrastColor}"/>
                    </TextBlock.Foreground>
                </TextBlock>
                <TextBlock Text="{Binding FolderID}" HorizontalAlignment="Left" TextWrapping="Wrap" VerticalAlignment="Top" Height="34" Width="496"  FontSize="24" Margin="0,72,0,0" Visibility="Collapsed"/>
                <TextBlock Text="{Binding ItemNumber}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="28" Width="387" FontSize="21" Margin="109,34,0,0" FontFamily="Segoe WP"/>
                <TextBlock Text="{Binding ItemTitle}" HorizontalAlignment="Left" VerticalAlignment="Top" Height="37" Width="387" FontSize="28" FontFamily="Segoe WP" Margin="109,0,0,0"/>
            </Grid>
        </Grid>
    </DataTemplate>

I've used XAML to bind my ListBox like: ItemsSource = "{Binding}" 我已经使用XAML像这样绑定ListBox: ItemsSource = "{Binding}"

Here's my INotifyPropertyChanged: 这是我的INotifyPropertyChanged:

 public class Items : INotifyPropertyChanged
    {
        private string _ItemTitle;
        public string ItemTitle
        {
            get { return _ItemTitle; }
            set
            {
                _ItemTitle = value;
                RaisePropertyChangedForItemTitle("ItemTitle");
            }
        }
        private string _ItemBody;
        public string ItemBody
        {
            get { return _ItemBody; }
            set
            {
                _ItemBody = value;
                RaisePropertyChangedForItemBody("ItemBody");
            }
        }
        private string _FolderID;
        public string FolderID
        {
            get { return _FolderID; }
            set
            {
                _FolderID = value;
                RaisePropertyChangedForItemFolderID("FolderID");
            }
        }
        private string _ItemNumber;
        public string ItemNumber
        {
            get { return _ItemNumber; }
            set
            {
                _ItemNumber = value;
                RaisePropertyChangedForItemNumber("ItemNumber");
            }
        }

        public event PropertyChangedEventHandler ItemTitleChanged;
        public void RaisePropertyChangedForItemTitle(string ItemTitle)
        {
            PropertyChangedEventHandler handler = ItemTitleChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(ItemTitle));
        }
        public event PropertyChangedEventHandler ItemBodyChanged;
        public void RaisePropertyChangedForItemBody(string ItemBody)
        {
            PropertyChangedEventHandler handler = ItemBodyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(ItemBody));
        }
        public event PropertyChangedEventHandler FolderIDChanged;
        public void RaisePropertyChangedForItemFolderID(string FolderID)
        {
            PropertyChangedEventHandler handler = FolderIDChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(FolderID));
        }
        public event PropertyChangedEventHandler ItemNumberChanged;
        public void RaisePropertyChangedForItemNumber(string ItemNumber)
        {
            PropertyChangedEventHandler handler = ItemNumberChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(ItemNumber));
        }
    }

And this is the ObservableCollection: 这是ObservableCollection:

public class AddItems : ObservableCollection<Items>
    {
        IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
        public AddItems()
        {
            string directory = "/GetItems";
            string[] filenames = store.GetDirectoryNames(directory);
            foreach (string filename in filenames)
            {
                IsolatedStorageFileStream openName = store.OpenFile("/GetItems/contactName.txt", FileMode.Open, FileAccess.Read);
                IsolatedStorageFileStream openNumber = store.OpenFile("/GetItems/contactNumber.txt", FileMode.Open, FileAccess.Read);
                IsolatedStorageFileStream openFolderID = store.OpenFile("/GetItems/FolderID.txt", FileMode.Open, FileAccess.Read);
                IsolatedStorageFileStream openNotes = store.OpenFile("/GetItems/contactNotes.txt", FileMode.Open, FileAccess.Read);

                using (StreamReader readContactName = new StreamReader(openName))
                {
                    var contactName = readContactName.ReadLine();
                    using (StreamReader readContactNumber = new StreamReader(openNumber))
                    {
                        var contactNumber = readContactNumber.ReadLine();
                        using (StreamReader ReadFolderID = new StreamReader(openFolderID))
                        {
                            var FolderID = ReadFolderID.ReadLine();
                            using (StreamReader ReadContactNotes = new StreamReader(openNotes))
                            {
                                var contactNotes = ReadContactNotes.ReadLine();
                                Add(new Items() { ItemTitle = contactName, ItemBody = contactNotes, FolderID = FolderID, ItemNumber = contactNumber });
                            }
                        }
                    }
                }
            }
        }

This is how I load my items into the ListBox: listBox.DataContext = collections.LoadItems; 这是我将项目加载到ListBox中的方式: listBox.DataContext = collections.LoadItems; I've added my INotifyPropertyChanged and ObservableCollection into a different classfile called Collections.cs hence the Collections.LoadItems. 我将INotifyPropertyChanged和ObservableCollection添加到了另一个名为Collections.cs的类文件中,因此添加了Collections.LoadItems。

Now I start the app and the items will load. 现在,我启动了应用程序,项目将加载。 I click on an item and I go to edit that and I save the contents and I go back to the Mainpage and the Listbox will show the old contents. 我单击一个项目,然后进行编辑,然后保存内容,然后返回主页,列表框将显示旧内容。 Now for me to get the edited new content, I have to close the app and relaunch it and that way it will show me the new content. 现在,要获取编辑后的新内容,我必须关闭该应用程序并重新启动它,这样它将向我显示新内容。

This is not right obviously because the INotifyPropertyChanged should automatically change the items value etc.. and show the latest content. 这显然是不对的,因为INotifyPropertyChanged应该自动更改项目值等,并显示最新内容。 If someone can please help me solve this because I'm just really clueless. 如果有人可以帮我解决这个问题,因为我真的很笨。 Not sure if I'm even doing it right. 不知道我是否做对了。

Greatly appreciated! 不胜感激! Thanks! 谢谢!

The pattern for using INotifyPropertyChanged is different than what you've used. 使用INotifyPropertyChanged的模式与您使用的模式不同。 Instead of having individual events for each property, there's just one event that is raised for any property changes: 除了每个属性都没有单独的事件,任何属性更改都只会引发一个事件:

public class Items : INotifyPropertyChanged
{
    // all property changes use this event
    public event PropertyChangedEventHandler PropertyChanged;
    private string _ItemTitle;

    // one property as an example:
    public string ItemTitle
    {
        get { return _ItemTitle; }
        set
        {
            _ItemTitle = value;
            // by using the CallerMemberName attribute
            // you can skip passing the specific property name 
            // as it's added automatically 
            RaisePropertyChanged();
        }
    }

    private void RaisePropertyChanged([CallerMemberName] String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }                  
}

There must be more code than you've shown, as your class wouldn't have compiled as shown without the declaration for the event PropertyChanged : 必须有比您显示的更多的代码,因为如果没有对事件PropertyChanged的声明,则您的类将无法如所示进行编译:

public event PropertyChangedEventHandler PropertyChanged;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用INotifyPropertyChanged更新ObservableCollection项属性 - Updating ObservableCollection Item properties using INotifyPropertyChanged 在没有INotifyPropertyChanged的情况下更新ObservableCollection成员属性的绑定 - Updating binding for ObservableCollection member property without INotifyPropertyChanged TreeViewItem不会使用绑定的ItemsSource,ObservableCollection和INotifyPropertyChanged更新 - TreeViewItem not updating with bound ItemsSource, ObservableCollection, and INotifyPropertyChanged 虽然我实现了INotifyPropertyChanged,但ObservableCollection没有更新 - ObservableCollection not updating though I INotifyPropertyChanged is implemented INotifyPropertyChanged 和 ObservableCollection WPF - INotifyPropertyChanged and ObservableCollection WPF 使用 INotifyPropertyChanged 而不是 ObservableCollection - using INotifyPropertyChanged instead of ObservableCollection MVVM中的ObservableCollection和INotifyPropertyChanged问题 - Problem with ObservableCollection and INotifyPropertyChanged in MVVM 绑定ObservableCollection是否需要INotifyPropertyChanged? - Is INotifyPropertyChanged needed for binding ObservableCollection? 将具有INotifyProperty的ObservableCollection更改为DatagridComboboxColumn绑定 - ObservableCollection with INotifyPropertyChanged into DatagridComboboxColumn Binding 在ObservableCollection上的WPF UserControl DependencyProperty和INotifyPropertyChanged - WPF UserControl DependencyProperty and INotifyPropertyChanged on an ObservableCollection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM