Reputation: 487
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.
This is my XAML for the ListBox:
<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}"
Here's my 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:
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;
I've added my INotifyPropertyChanged and ObservableCollection into a different classfile called Collections.cs hence the 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. 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!
Upvotes: 0
Views: 234
Reputation: 59793
The pattern for using INotifyPropertyChanged
is different than what you've used. 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
:
public event PropertyChangedEventHandler PropertyChanged;
Upvotes: 3