[英]UWP ComboBox SelectedValue not being set
I obviously don't grasp using SelectedValue to change which item a combo box is showing in UWP 我显然没有掌握使用SelectedValue来改变组合框在UWP中显示的项目
The XAML is simple XAML很简单
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ComboBox x:Name="comboBoxColor" />
</Grid>
The code-behind isn't very deep 代码隐藏不是很深
public MainPage()
{
this.InitializeComponent();
}
public void page_Loaded(object sender, RoutedEventArgs e)
{
populateDdlMultiColor();
comboBoxColor.SelectedValue = Colors.Red;
//comboBoxColor.SelectedIndex = 0 works tho
}
private void populateDdlMultiColor()
{
comboBoxColor.ItemsSource = ColorDict();
comboBoxColor.DisplayMemberPath = "Key";
comboBoxColor.SelectedValuePath = "Value";
}
private Dictionary<string,Color> ColorDict()
{
Dictionary<string, Color> L = new Dictionary<string, Color>();
L.Add("reddish",Colors.Red);
return L;
}
This is obviously tinker-toy but it fails the same way my code fails: After setting the SelectedValue, the combo box is on index -1 and SelectedValue is null. 这显然是修补玩具,但它失败的方式与我的代码失败相同:设置SelectedValue后,组合框的索引为-1,SelectedValue为null。 If I set SelectedIndex to a proper value [see comment] the combo box works - it has been loaded. 如果我将SelectedIndex设置为适当的值[请参阅注释]组合框工作 - 它已被加载。
Thanks for your feedback. 感谢您的反馈意见。 This is a known issue that SelectedValue doesn't work with enumeration type. 这是一个已知问题, SelectedValue不适用于枚举类型。
For now, as a workaround, you can use SelectedIndex or SelectedItem like you've done. 目前,作为一种变通方法,您可以像使用的那样使用SelectedIndex或SelectedItem 。 However, please note that Dictionary
is implemented as a hash table. 但请注意, Dictionary
实现为哈希表。 There's no such concept of an "index" within a Dictionary
. Dictionary
没有“索引”这样的概念。 The order is undefined and we can't rely on it. 订单未定义,我们不能依赖它。 So for your scenario, using SelectedItem
might be better. 因此,对于您的场景,使用SelectedItem
可能会更好。
For more info, you can see this answer . 有关详细信息,您可以看到此答案 。
Thanks to Jay Zuo for putting me on the right track! 感谢Jay Zuo让我走上正轨! In my scenario, it's then easy enough to convert the colors to integers as a workaround to the known issue. 在我的场景中,然后很容易将颜色转换为整数作为已知问题的变通方法。 Won't work for every enumeration, but anyone building color dropdowns [in UWP] may find this handy. 不适用于每个枚举,但任何建立颜色下拉的人[在UWP]可能会发现这个方便。 Note the Dictionary change. 注意字典更改。
public MainPage()
{
this.InitializeComponent();
}
public void page_Loaded(object sender, RoutedEventArgs e)
{
populateDdlMultiColor();
comboBoxColor.SelectedValue = ColorInt(Colors.Red);
}
private void populateDdlMultiColor()
{
comboBoxColor.ItemsSource = ColorDict();
comboBoxColor.DisplayMemberPath = "Key";
comboBoxColor.SelectedValuePath = "Value";
}
private Dictionary<string,int> ColorDict()
{
Dictionary<string, int> L = new Dictionary<string, int>();
L.Add("reddish",ColorInt(Colors.Red));
return L;
}
private int ColorInt(Color c)
{
return (c.A*16777216) + (c.R*65536) + (c.G*256) + c.B ;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.