我已将复选框的已检查状态保存在隔离存储中,但每当我取消选中该复选框,然后再次启动应用程序时,它仍然会被检查,但我将其状态保存在backkeypress上
这是来自Constructor
if (IsolatedStorageSettings.ApplicationSettings.Contains("mySavedList"))
{
lstshopNew = IsolatedStorageSettings.ApplicationSettings["mySavedList"] as List<Shopping>;
if (lstshopNew.Count > 0)
{
foreach (var item in lstshopNew)
{
var t = lstShop.Where(s => s.list.Equals(item.list)).FirstOrDefault();
if (t != null)
{
t.IsCompleted = true;
}
}
private void checkboxSL_Checked(object sender, RoutedEventArgs e)
{
//isChkboxChecked = true;
//(sender as CheckBox).IsChecked = isChkboxChecked;
//IsolatedStorageSettings.ApplicationSettings["isChecked"] = (sender as CheckBox).IsChecked;
var chk = (sender as CheckBox).DataContext as Shopping;
if (chk == null)
{
return;
}
chk.IsCompleted = true;
var lstItem = lstShop.Where(x => x.list.Equals(chk.list)).FirstOrDefault();
if (lstItem != null)
{
lstshopNew.Remove(chk);
}
lstshopNew.Add(chk);
}
private void checkboxSL_Unchecked(object sender, RoutedEventArgs e)
{
//(sender as CheckBox).IsChecked = false;
//IsolatedStorageSettings.ApplicationSettings["isChecked"] = (sender as CheckBox).IsChecked;
var chk = (sender as CheckBox).DataContext as Shopping;
if (chk == null)
{
return;
}
chk.IsCompleted = false;
var lstItem = lstShop.Where(x => x.list.Equals(chk.list)).FirstOrDefault();
if (lstItem != null)
{
lstshopNew.Remove(chk);
}
lstshopNew.Remove(chk);
}
protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
{
base.OnBackKeyPress(e);
IsolatedStorageSettings.ApplicationSettings["mySavedList"] = lstshopNew;
}
<ListBox x:Name="listBox2" SelectionMode="Multiple" Grid.Row="1" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Background="White" >
<CheckBox x:Name="checkboxSL" IsChecked="{Binding IsCompleted, Mode=TwoWay}" Checked="checkboxSL_Checked" Unchecked="checkboxSL_Unchecked" BorderBrush="Black" Foreground="Black" Style="{StaticResource CheckBoxStyle1}"/>
<TextBlock Text="{Binding list}" FontSize="30" TextWrapping="Wrap" HorizontalAlignment="Left" Foreground="Black" Margin="0,15,0,0"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
答案 0 :(得分:0)
您将Shopping
放入隔离存储空间。这是具有唯一内存地址的C#类的实例。然后,您重新运行该应用程序并检查保存的Shopping对象是否与您刚刚创建的Shopping对象相同。事实并非如此,因为这些新对象是具有新内存地址的新实例。
您要做的是将唯一ID(如数字或唯一关键字)放入Shopping类中。然后比较该ID(不是整个对象)以确定存储中的对象是否与GUI上的对象相同。
答案 1 :(得分:0)
我对为我工作的事件做了这些修改 private void checkboxSL_Checked(object sender,RoutedEventArgs e) { var chk =(发送者为CheckBox).DataContext为Shopping;
if (chk == null)
{
return;
}
chk.IsCompleted = true;
var lstItem = lstShop.Where(x => x.list.Equals(chk.list)).FirstOrDefault();
//if (lstItem != null)
//{
// lstshopNew.Remove(lstItem);
//}
if (lstshopNew.Count > 0)
{
foreach (var item in lstshopNew.ToList())
{
if (item.list.Equals(lstItem.list))
{
tItem = item.list;
}
}
if (!lstItem.list.Equals(tItem))
{
lstshopNew.Add(chk);
}
}
else
{
lstshopNew.Add(chk);
}
}
private void checkboxSL_Unchecked(object sender, RoutedEventArgs e)
{
var chk = (sender as CheckBox).DataContext as Shopping;
if (chk == null)
{
return;
}
chk.IsCompleted = false;
var lstItem = lstShop.Where(x => x.list.Equals(chk.list)).FirstOrDefault();
if (lstItem != null)
{
foreach (var item in lstshopNew.ToList())
{
if (item.list.Equals(lstItem.list))
{
lstshopNew.Remove(item);
}
}
}
}