我一直试图让它工作但我仍然没有运气..基本上我使用ObservableColletion将我的所有项目加载到一个具有自定义ItemTemplate的列表框中。
这是我对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>
我使用XAML绑定我的ListBox,如:ItemsSource = "{Binding}"
这是我的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));
}
}
这是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 });
}
}
}
}
}
}
这是我怎样加载我项目到列表框:listBox.DataContext = collections.LoadItems;
我已经添加我的INotifyPropertyChanged的和的ObservableCollection到名为Collections.cs因此Collections.LoadItems一个的不同的类文件
现在我启动应用程序并加载项目。我点击一个项目然后我去编辑它并保存内容然后我回到主页面,列表框将显示旧内容。现在,我要获取已编辑的新内容,我必须关闭应用程序并重新启动它,这样它将向我显示新内容。
这显然是不对的,因为INotifyPropertyChanged应该自动更改项目值等..并显示最新内容。如果有人能帮我解决这个问题,因为我真的很无能为力。不确定我是否做得对。
非常感谢! 谢谢!
答案 0 :(得分:3)
使用INotifyPropertyChanged
的模式与您使用的模式不同。不是为每个属性设置单独的事件,而是为任何属性更改引发一个事件:
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));
}
}
}
必须有比您显示的代码更多的代码,因为如果没有事件PropertyChanged
的声明,您的类将不会如图所示进行编译:
public event PropertyChangedEventHandler PropertyChanged;