gpt4 book ai didi

c# - UWP 反射(reflect)通过绑定(bind)从设置中加载的 ComboBox 选定项

转载 作者:太空狗 更新时间:2023-10-29 23:06:26 24 4
gpt4 key购买 nike

这是 an earlier question 的延续.

对于某些应用程序设置,我想使用 ComboBox 来选择一个选项。我可以将所选选项保存到(漫游)设置并再次加载。加载的选项正确显示在 TextBlock 中,但 ComboBox 显示空白。我怎样才能在 ComboBox 中反射(reflect)加载的当前选择的选项?

这是 XAML:

<Page
x:Class="ComboBoxTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:ComboBoxTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:converter="using:ComboBoxTest.Converter"
mc:Ignorable="d">

<Page.Resources>
<converter:ComboBoxItemConvert x:Key="ComboBoxItemConvert" />
</Page.Resources>


<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<StackPanel>
<ComboBox
Name="ComboBox"
ItemsSource="{x:Bind ComboBoxOptions}"
SelectedItem="{x:Bind SelectedComboBoxOption, Mode=TwoWay, Converter={StaticResource ComboBoxItemConvert}}"
SelectedValuePath="ComboBoxOption"
DisplayMemberPath="ComboBoxHumanReadableOption"
Header="ComboBox" >
</ComboBox>
<TextBlock Name="BoundTextblock" Text="{x:Bind SelectedComboBoxOption.ComboBoxOption, Mode=OneWay}"/>
</StackPanel>
</Grid>

这是背后的代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Xml.Serialization;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace ComboBoxTest
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page, INotifyPropertyChanged
{
ApplicationDataContainer roamingSettings = null;

private ObservableCollection<ComboBoxItem> ComboBoxOptions;

public MainPage()
{
this.InitializeComponent();
ComboBoxOptions = new ObservableCollection<ComboBoxItem>();
ComboBoxOptionsManager.GetComboBoxList(ComboBoxOptions);


roamingSettings = ApplicationData.Current.RoamingSettings;


var value = (string)roamingSettings.Values["ComboBoxSelection"];
if (value != null)
{
SelectedComboBoxOption = Deserialize<ComboBoxItem>(value); //loaded selection reflected in the textbox but not in the ComboBox
}
else
{
SelectedComboBoxOption = ComboBoxOptions[0];
}

}

public class ComboBoxItem
{
public string ComboBoxOption { get; set; }
public string ComboBoxHumanReadableOption { get; set; }
}

public class ComboBoxOptionsManager
{
public static void GetComboBoxList(ObservableCollection<ComboBoxItem> ComboBoxItems)
{
var allItems = getComboBoxItems();
ComboBoxItems.Clear();
allItems.ForEach(p => ComboBoxItems.Add(p));
}

private static List<ComboBoxItem> getComboBoxItems()
{
var items = new List<ComboBoxItem>();

items.Add(new ComboBoxItem() { ComboBoxOption = "Option1", ComboBoxHumanReadableOption = "Option 1" });
items.Add(new ComboBoxItem() { ComboBoxOption = "Option2", ComboBoxHumanReadableOption = "Option 2" });
items.Add(new ComboBoxItem() { ComboBoxOption = "Option3", ComboBoxHumanReadableOption = "Option 3" });

return items;
}
}

private ComboBoxItem _SelectedComboBoxOption;

public ComboBoxItem SelectedComboBoxOption
{
get
{
return _SelectedComboBoxOption;
}
set
{
if (_SelectedComboBoxOption != value)
{
_SelectedComboBoxOption = value;
roamingSettings.Values["ComboBoxSelection"] = Serialize(value);
RaisePropertyChanged("SelectedComboBoxOption");
}
}
}




public static string Serialize(object obj)
{
using (var sw = new StringWriter())
{
var serializer = new XmlSerializer(obj.GetType());
serializer.Serialize(sw, obj);
return sw.ToString();
}
}

public static T Deserialize<T>(string xml)
{
using (var sw = new StringReader(xml))
{
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(sw);
}
}


void RaisePropertyChanged(string prop)
{
if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(prop)); }
}
public event PropertyChangedEventHandler PropertyChanged;
}
}

最佳答案

这是一个典型的项目案例,看起来相同,但由于它们有不同的引用(请阅读 C# reference types)。

您使用 3 个值加载 ComboBox,这 3 个值显示在下拉列表中。要在 ComboBox 关闭时查看所选项目,它必须是(=具有相同的引用)这 3 个值之一。当您的漫游设置中没有保存任何内容时,您可以选择第一个作为 SelectedItem。切换到另一个选定项时,您还将在 SelectedItem 属性中拥有一个有效引用。

但是,当您反序列化保存的 RoamingSettings 值时,您会创建一个具有不同引用的新对象。当您将此项目设置为 SelectedItem 时,ComboBox 控件将不会在其项目中找到它,因此不会选择项目。

要解决此问题,您必须在 ItemSource 集合中找到正确的项目:

var value = (string)roamingSettings.Values["ComboBoxSelection"];
if (value != null)
{
var deserialized = Deserialize<ComboBoxItem>(value);
// using ComboBoxOption as the primary key field of your object
SelectedComboBoxOption = ComboBoxOptions.SingleOrDefault(c =>
c.ComboBoxOption == deserialized.ComboBoxOption);
}
else
{
SelectedComboBoxOption = ComboBoxOptions[0];
}

关于c# - UWP 反射(reflect)通过绑定(bind)从设置中加载的 ComboBox 选定项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33856882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com