保存包含wp7复选框的列表框状态

时间:2013-04-27 11:43:51

标签: windows-phone-7

我创建了一个按钮来创建多个复选框。 wp7的点击次数。在我用于它的代码之下。

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
 <TextBox x:Name="txtNewTask" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" VerticalAlignment="Top" Width="328"/>
 <Button x:Name="btnAdd" Content="add" HorizontalAlignment="Left" Margin="328,0,0,0" VerticalAlignment="Top" Width="123" Click="btnAdd_Click"/>
 <ListBox x:Name="lbToDoList" Margin="0,72,0,0">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <CheckBox Click="CheckBox_Click" Background="{x:Null}">
         <StackPanel Orientation="Horizontal">
           <TextBlock Text="{Binding}" Name="tbkTextName" VerticalAlignment="Center" Margin="5,0,5,0" />
         </StackPanel>
       </CheckBox>
     </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>
</Grid>

现在,当我退出并重新打开我的应用时,复选框将被取消选中(默认状态),并且不会保存其状态。如果我使用x:Name作为该复选框,那么我可以保存状态,但由于所有复选框都分配了相同的名称,因此所有复选框状态都变为相同。

应用有效时

When App is active

重新启动应用时

When App is reactivated

有人可以帮我保存列表框中的复选框状态吗?

1 个答案:

答案 0 :(得分:0)

这应该做:

XAML:

<TextBox x:Name="txtNewTask" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" VerticalAlignment="Top" Width="328"/>
<Button x:Name="btnAdd" Content="add" HorizontalAlignment="Left" Margin="328,0,0,0" VerticalAlignment="Top" Width="123" Click="btnAdd_Click"/>
<ListBox Grid.Row="1" x:Name="lbToDoList" Margin="0,72,0,0">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding IsChecked,Mode=TwoWay}" Background="{x:Null}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" Name="tbkTextName" VerticalAlignment="Center" Margin="5,0,5,0" />
                </StackPanel>
            </CheckBox>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

代码背后:

public partial class MainPage:PhoneApplicationPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        lbToDoList.ItemsSource = IsolatedStorageHelper.Instance.Tasks;
    }

    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);
        IsolatedStorageHelper.Instance.Save();
    }

    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        IsolatedStorageHelper.Instance.Tasks.Add(new MyTask()
        {
            Name = txtNewTask.Text
        });
    }
}

public class IsolatedStorageHelper
{
    public static IsolatedStorageHelper Instance = new IsolatedStorageHelper();
    private IsolatedStorageSettings Settings = IsolatedStorageSettings.ApplicationSettings;
    public ObservableCollection<MyTask> _tasks;
    public ObservableCollection<MyTask> Tasks
    {
        get
        {
            if (_tasks == null)
            {
                if (!Settings.Contains("Tasks"))
                    Settings["Tasks"] = new List<MyTask>();
                _tasks = new ObservableCollection<MyTask>((List<MyTask>)Settings["Tasks"]);
                _tasks.CollectionChanged += _tasks_CollectionChanged;
            }
            return _tasks;
        }
    }

    void _tasks_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        Save();
    }

    public void Save()
    {
        Settings["Tasks"] = _tasks.ToList();
    }
}
public class MyTask
{
    public string Name { get; set; }
    public bool IsChecked { get; set; }
}