如何保存更改的 ComboBox 值并在下次加载页面时将其显示为 ComboBox 默认值
How to save changed ComboBox value and display it as the ComboBox default value the next time that the page is loaded
我需要保存用户在运行时更改的组合框值,并在下次加载页面时将其显示为组合框默认值。我正在为我的 UWP 应用程序询问这个。我在 Settings.xaml 中定义了组合框,如下所示:
<ComboBox Name="CLengthCombo" SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem Content="24"/>
<ComboBoxItem Content="25"/>
<ComboBoxItem Content="26" IsSelected="True"/>
<ComboBoxItem Content="27"/>
</ComboBox>
在我的 Settings.xaml.cs 中,我定义了一个名为 "localSettings_CycleLength" 的全局变量,以保存更改的组合框值,以便它在应用程序启动之间保留:
Windows.Storage.ApplicationDataContainer localSettings_CycleLength = Windows.Storage.ApplicationData.Current.LocalSettings;
然后我的 Settings.xaml.cs:
中有以下代码
public Settings()
{
this.InitializeComponent();
if (localSettings_CycleLength.Values["CycleLength"] != null)
{
this.CLengthCombo.SelectedItem = localSettings_CycleLength.Values["CycleLength"];
}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBoxItem = e.AddedItems[0] as ComboBoxItem;
if (comboBoxItem == null) return;
comboBoxItem.IsSelected = true;
var content = comboBoxItem.Content as string;
if (content != null)
{
localSettings_CycleLength.Values["CycleLength"] = content;
}
}
现在,上面的代码没有满足我的需要,我也不知道为什么。你能帮帮我吗?提前致谢!
您正在存储所选 ComboBoxItem
的内容。所以你不能将内容分配给 SelectedItem
你应该这样做
if (localSettings_CycleLength.Values.ContainsKey("CycleLength"))
{
var savedItem = localSettings_CycleLength.Values["CycleLength"]; ;
foreach(var item in CLengthCombo.Items)
{
if((item as ComboBoxItem).Content.Equals(savedItem ))
{
this.CLengthCombo.SelectedItem = item;
}
}
}
并且您的 xaml.So 中有 <ComboBoxItem Content="26" IsSelected="True"/>
每次您加载页面时,将选择第 26 项覆盖之前选择的项目
我需要保存用户在运行时更改的组合框值,并在下次加载页面时将其显示为组合框默认值。我正在为我的 UWP 应用程序询问这个。我在 Settings.xaml 中定义了组合框,如下所示:
<ComboBox Name="CLengthCombo" SelectionChanged="ComboBox_SelectionChanged">
<ComboBoxItem Content="24"/>
<ComboBoxItem Content="25"/>
<ComboBoxItem Content="26" IsSelected="True"/>
<ComboBoxItem Content="27"/>
</ComboBox>
在我的 Settings.xaml.cs 中,我定义了一个名为 "localSettings_CycleLength" 的全局变量,以保存更改的组合框值,以便它在应用程序启动之间保留:
Windows.Storage.ApplicationDataContainer localSettings_CycleLength = Windows.Storage.ApplicationData.Current.LocalSettings;
然后我的 Settings.xaml.cs:
中有以下代码public Settings()
{
this.InitializeComponent();
if (localSettings_CycleLength.Values["CycleLength"] != null)
{
this.CLengthCombo.SelectedItem = localSettings_CycleLength.Values["CycleLength"];
}
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var comboBoxItem = e.AddedItems[0] as ComboBoxItem;
if (comboBoxItem == null) return;
comboBoxItem.IsSelected = true;
var content = comboBoxItem.Content as string;
if (content != null)
{
localSettings_CycleLength.Values["CycleLength"] = content;
}
}
现在,上面的代码没有满足我的需要,我也不知道为什么。你能帮帮我吗?提前致谢!
您正在存储所选 ComboBoxItem
的内容。所以你不能将内容分配给 SelectedItem
你应该这样做
if (localSettings_CycleLength.Values.ContainsKey("CycleLength"))
{
var savedItem = localSettings_CycleLength.Values["CycleLength"]; ;
foreach(var item in CLengthCombo.Items)
{
if((item as ComboBoxItem).Content.Equals(savedItem ))
{
this.CLengthCombo.SelectedItem = item;
}
}
}
并且您的 xaml.So 中有 <ComboBoxItem Content="26" IsSelected="True"/>
每次您加载页面时,将选择第 26 项覆盖之前选择的项目