Windows Store 应用程序的 DisplayMemberPath="Value" 的替代方案是什么?
What is the alternative for DisplayMemberPath="Value" for Windows Store applications?
我认为 Windows Store Applications 在使用 DisplayMemberPath="Value".
时存在错误
这是我的代码
<ComboBox Height="40" VerticalAlignment="Stretch" SelectedValuePath="Key" DisplayMemberPath="Value" x:Name="comboBox1" FontSize="25"/>
var source = new Dictionary<string, double>();
source.Add("Item1", 0.4);
source.Add("Item2", 0.3);
source.Add("Item3", 0.1);
source.Add("Item4", 0.1);
var formateDSource = new Dictionary<string, string>();
foreach (var item in source)
{
formateDSource.Add(string.Format("[{0}, {1}]", item.Key, item.Value), item.Key);
}
comboBox1.ItemsSource = source;
如果您在 WPF 中使用此代码,则效果很好。但是,如果您在 Windows 商店应用程序中使用此代码,则组合框为空并引发错误。那么在 Windows Store Applications 中是否有替代方法来执行此操作并且我发现了一个错误?因为我已经研究 Web 好几天了,但没有找到解决方案。*请不要发表评论,除非您已经尝试将我的代码作为 Windows Store App 而不是 Visual Studios 中的 WPF。
我已经能够在通用应用程序(Windows phone 版本)中重现它。
要解决它,首先删除组合框上的 "Height" 属性 值,因为它会阻止组合框在您打开它时向您显示选项(在 Windows Phone 至少)。
然后,在后面的代码中,尝试将您的 Dictionnary 转换为对象列表(或另一个 Dictionnary)(我使用匿名对象,但您最好创建一个自定义类型):
comboBox1.ItemsSource = formateDSource.Select(f => new { Key = f.Key, Value = f.Value }).ToList();
所以我的 xaml 看起来像这样:
<ComboBox x:Name="comboBox1"
VerticalAlignment="Stretch"
DisplayMemberPath="Value"
FontSize="25"
SelectedValuePath="Key" />
现在,我不太明白原始词典是怎么回事,但至少你现在应该有一个解决方法。
我认为 Windows Store Applications 在使用 DisplayMemberPath="Value".
时存在错误这是我的代码
<ComboBox Height="40" VerticalAlignment="Stretch" SelectedValuePath="Key" DisplayMemberPath="Value" x:Name="comboBox1" FontSize="25"/>
var source = new Dictionary<string, double>();
source.Add("Item1", 0.4);
source.Add("Item2", 0.3);
source.Add("Item3", 0.1);
source.Add("Item4", 0.1);
var formateDSource = new Dictionary<string, string>();
foreach (var item in source)
{
formateDSource.Add(string.Format("[{0}, {1}]", item.Key, item.Value), item.Key);
}
comboBox1.ItemsSource = source;
如果您在 WPF 中使用此代码,则效果很好。但是,如果您在 Windows 商店应用程序中使用此代码,则组合框为空并引发错误。那么在 Windows Store Applications 中是否有替代方法来执行此操作并且我发现了一个错误?因为我已经研究 Web 好几天了,但没有找到解决方案。*请不要发表评论,除非您已经尝试将我的代码作为 Windows Store App 而不是 Visual Studios 中的 WPF。
我已经能够在通用应用程序(Windows phone 版本)中重现它。
要解决它,首先删除组合框上的 "Height" 属性 值,因为它会阻止组合框在您打开它时向您显示选项(在 Windows Phone 至少)。
然后,在后面的代码中,尝试将您的 Dictionnary 转换为对象列表(或另一个 Dictionnary)(我使用匿名对象,但您最好创建一个自定义类型):
comboBox1.ItemsSource = formateDSource.Select(f => new { Key = f.Key, Value = f.Value }).ToList();
所以我的 xaml 看起来像这样:
<ComboBox x:Name="comboBox1"
VerticalAlignment="Stretch"
DisplayMemberPath="Value"
FontSize="25"
SelectedValuePath="Key" />
现在,我不太明白原始词典是怎么回事,但至少你现在应该有一个解决方法。