0

我创建了一个按钮来创建多个复选框。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该复选框,那么我可以保存状态,但由于所有复选框都分配给相同的名称,因此所有复选框状态都变得相同。

当应用程序处于活动状态时

当应用程序处于活动状态时

重新激活应用程序时

重新激活应用程序时

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

4

1 回答 1

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; }
}
于 2014-04-14T20:32:31.017 回答