如何在 ListBox windows phone 8 中保存复选框状态 [英] How to Save Checkboxes state in ListBox windows phone 8

查看:25
本文介绍了如何在 ListBox windows phone 8 中保存复选框状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在列表框中有一个列表框和一个复选框.XAML代码如下

I have a list box and a checkbox inside list box. XAML code is as follows

  <ListBox Name="list_locationAddFinal"  ItemContainerStyle="{StaticResource ListBoxCheckedStyle}" LayoutUpdated="list_locationAddFinal_LayoutUpdated" Foreground="Black" Visibility="Collapsed">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <CheckBox   Visibility="Visible" Background="Black" IsChecked="{Binding IsChecked,Mode=TwoWay}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"/>
                            <TextBlock x:Name="item_name" Text="{Binding Name, Mode=OneWay}" Padding="5,15,5,15" Width="400"  TextWrapping="Wrap" FontSize="{StaticResource PhoneFontSizeLarge}"/>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

现在,当用户选中复选框时,我想保存选中状态,当我打开应用程序时,复选框未显示为选中状态谢谢

now when user will check a checkbox i want to save checked state and when i will open my application the checkbox box is not showing checked thanks

我的后端代码如下

 public class SampleCheckedData
{
    public bool IsChecked
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }

    public string Icon
    {
        get;
        set;
    }
}
 private void CheckBox_Checked(object sender, RoutedEventArgs e)
    {
        ListBoxItem checedItem = this.list_locationAddFinal.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem;
        if (checedItem != null)
        {
            checedItem.IsSelected = true;
        }
    }`
  private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
    {
        ListBoxItem checedItem = this.list_locationAddFinal.ItemContainerGenerator.ContainerFromItem((sender as CheckBox).DataContext) as ListBoxItem;
        if (checedItem != null)
        {
            checedItem.IsSelected = false;
        }
    }
}

推荐答案

每当您启动应用程序时,所有变量/控件等都会初始化为其默认值.

Whenever you launch your App, all the variables/controls etc get initialized to their default values.

为了保存"您的 Checkbox 状态,您需要将信息存储到一个并不总是被初始化的地方,或者简单地说不重置".

In order to "save" your Checkbox's state, you need to store the information into a place that does not always get initialized or in simpler words "does not reset".

实现这一目标的一种方法是使用IsolatedStorage.它类似于我们在 C 和 C++ 等一些基本编程语言中使用的数据文件处理概念.

One way in which this can be achieved is by using the IsolatedStorage. It's similar to the Data-File Handling concepts that we use in some basic programming languages like C and C++.

使用复选框的 Checked 和 Unchecked 事件将特定值写入您的文件.在页面的加载事件中,您可以随时打开此文件以检查其值并相应地设置控件的状态.

您可以在此处找到一个 IndependentStorage 示例一>.

复选框事件的代码有点像这样:-

The code for the Checkbox's events are somewhat like this:-

private void CHK_Checked(object sender, RoutedEventArgs e)
        {
            var appStorage2 = IsolatedStorageFile.GetUserStoreForApplication();

            string filename2 = "stored_value.txt";
            using (var file = appStorage2.OpenFile(filename2, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
            {

                using (var writer = new StreamWriter(file))
                {
                    writer.Write("1");
                }
            }
        }

        private void CHK_Unchecked(object sender, RoutedEventArgs e)
        {
            var appStorage2 = IsolatedStorageFile.GetUserStoreForApplication();

            string filename2 = "stored_value.txt";
            using (var file = appStorage2.OpenFile(filename2, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
            {

                using (var writer = new StreamWriter(file))
                {
                    writer.Write("0");
                }
            }
        }

这篇关于如何在 ListBox windows phone 8 中保存复选框状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆