[英]WinRT ListView with itemsource as List<KeyValuePair<string, string>> error XAML (Windows 8.1 Universal App (XAML/C#))
I cant get this to work... (Windows 8 Universal App (XAML/C#) project) I have a listview that is bound to a List < KeyValuePair < string , string > > 我无法使它正常工作...(Windows 8通用应用程序(XAML / C#)项目)我有一个绑定到List <KeyValuePair <string,string>>的列表视图
here is the code: 这是代码:
<ListView
ItemsSource="{Binding Languages}"
SelectedItem="{Binding SelectedLanguage, Mode=TwoWay}"
>
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}" FontSize="18" FontWeight="Light" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
and here is my C# code: 这是我的C#代码:
private List<KeyValuePair<string, string>> _languages;
public List<KeyValuePair<string, string>> Languages
{
get
{
if (_languages == null)
{
_languages = Utils.AvailableLanguages;
}
return _languages;
}
}
public KeyValuePair<string, string> SelectedLanguage
{
get
{
return Languages.SingleOrDefault(x => x.Key == Utils.CurrentLanguage);
}
set
{
Utils.CurrentLanguage = value.Key;
Api.Localization = value.Key;
RaisePropertyChanged(() => CurrentLanguage);
}
}
In my listview if I do a "Binding Value" I get nothing (no data is shown), however if i do Binding without the Value element it shows me both the Key and the Value of my List : 在我的列表视图中,如果我执行“绑定值”,我什么也没得到(未显示任何数据),但是,如果我在没有Value元素的情况下执行绑定,则显示键和列表的值:
<TextBlock Text="{Binding}" FontSize="18" FontWeight="Light" />
it showns me this: 它告诉我这个:
How do i make it so that i show only the Value of my list and not the key? 我如何做到这一点,以便仅显示列表的值而不显示键? thank you for your help!
谢谢您的帮助!
Edit: if i do this: 编辑:如果我这样做:
<TextBlock Text="{Binding key}" FontSize="18" FontWeight="Light" />
or that 或者那个
<TextBlock Text="{Binding value}" FontSize="18" FontWeight="Light" />
same bug, : 同样的错误:
Edit2: Utils.AvailableLanguages value: Edit2:Utils.AvailableLanguages值:
Try 尝试
{Binding key} or {Binding value}
you can reduce code by this 您可以以此减少代码
get
{
return _languages ?? (_languages = Utils.AvailableLanguages);
}
It does indeed seem like the binding can't extract the property values - possibly because the types are generic. 确实看起来绑定似乎无法提取属性值-可能是因为类型是通用的。 This is what you can see in VS Output window:
这是您在“ VS输出”窗口中看到的内容:
`A first chance exception of type 'System.Reflection.TargetException' occurred in mscorlib.dll
Error: Cannot get 'Value' value (type 'String') from type 'System.Runtime.InteropServices.WindowsRuntime.CLRIKeyValuePairImpl`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. BindingExpression: Path='Value' DataItem='System.Runtime.InteropServices.WindowsRuntime.CLRIKeyValuePairImpl`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'; target element is 'Windows.UI.Xaml.Controls.TextBlock' (Name='null'); target property is 'Text' (type 'String').
key
and value
would be the fields of KeyValuePair
while bindings only work with properties, so that wouldn't work. key
和value
将是KeyValuePair
的字段,而绑定仅与属性一起使用,因此将不起作用。 The simple solution is to not use KeyValuePair
. 简单的解决方案是不使用
KeyValuePair
。 You should instead create your own class to hold the language code and country name. 您应该创建自己的类来保存语言代码和国家/地区名称。 If they are both unique - you could simply use a list of strings like this:
如果它们都是唯一的-您可以简单地使用以下字符串列表:
XAML XAML
<Page
x:Class="App51.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView
x:Name="lv">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
C# C#
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml.Controls;
namespace App51
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var kvps = new List<KeyValuePair<string, string>>();
kvps.Add(new KeyValuePair<string, string>("ar", "Argentina"));
kvps.Add(new KeyValuePair<string, string>("au", "Australia"));
kvps.Add(new KeyValuePair<string, string>("be-fr", "Belgique"));
this.lv.ItemsSource = kvps.Select(kvp => kvp.Value).ToList();
}
}
}
If there are fewer values than keys - you'd have to do something more like this: 如果值比键少-您必须执行以下操作:
XAML XAML
<Page
x:Class="App51.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView
x:Name="lv">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Value}"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Page>
C# C#
using System.Collections.Generic;
using System.Linq;
using Windows.UI.Xaml.Controls;
namespace App51
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
var kvps = new List<KeyValuePair<string, string>>();
kvps.Add(new KeyValuePair<string, string>("ar", "Argentina"));
kvps.Add(new KeyValuePair<string, string>("au", "Australia"));
kvps.Add(new KeyValuePair<string, string>("be-fr", "Belgique"));
this.lv.ItemsSource = kvps.Select(kvp => new { kvp.Key, kvp.Value }).ToList();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.