简体   繁体   中英

How to get previous checkbox state in windows phone

I have a checkbox under a listbox using the given xaml file . My xaml file:

<ListBox x:Name="notificationSettingsListBox" Grid.Row="1"   Margin="20,20,20,20" Background="#e79e38"  >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Background="#055cc3" Width="500" Height="200" Margin="30,40,30,20">
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding channel_name}" Foreground="White" FontSize="31" TextAlignment="Left" TextWrapping="Wrap" Margin="0,20,10,0" />
                        <CheckBox x:Name="pushNotiOnCheckBox" Content="Enable Notification"  Checked="pushNotiOnCheckBox_Checked" Unchecked="pushNotiOnCheckBox_Unchecked"/>
                    </StackPanel>

                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

Suppose in my listbox i have 5 checkbox and the user just checks 2 of the checkbox. Now when the user lunches the app in next time it will show the checked state of these 2 checkbox which he previously checked.

How can i achieve that using these xaml file in windows phone ??

You can store the selected values in the settings. This settings are persisted by the system and you can read the values by starting your app:

Code sample (save):

var settings = IsolatedStorageSettings.ApplicationSettings;

// txtInput is a TextBox defined in XAML.
if (!settings.Contains("userData"))
{
    settings.Add("userData", txtInput.Text);
}
else
{
    settings["userData"] = txtInput.Text;
}

settings.Save();

Code sample (read):

// txtDisplay is a TextBlock defined in XAML.
if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
{
    txtDisplay.Text = IsolatedStorageSettings.ApplicationSettings["userData"] as string;
}

More Infos: See this msdn article

Then, when you start your app/show the view: You just need to check, which values are checked in the settings and then mark the CheckBox as checked. When the Checkboxes are dynamic (not static) you better make a ViewModel to achive this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM