简体   繁体   English

WinRT ListView,将itemsource作为列表 <KeyValuePair<string, string> &gt;错误XAML(Windows 8.1通用应用程序(XAML / C#))

[英]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: 它告诉我这个: http://i.imgur.com/F5ecYJI.png

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. keyvalue将是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.

相关问题 使用C#和XAML更新或刷新Windows Phone 8.1应用程序中的Listview - Update or refresh Listview in Windows Phone 8.1 app with C# and XAML 在Windows Universal App中使用C#在XAML中着墨 - Inking in XAML with C# in a Windows Universal App 2对单个C#类的XAML引用(通用App 8.1) - 2 XAML reference to single C# class (Universal App 8.1) 绑定列表时 XAML 标记错误<string>到 WPF 中的 ComboBox ItemSource - XAML markup error when binding List<string> to a ComboBox ItemSource in a WPF 如何序列化和反序列化字典 <string,object> 在WinRT中(C#和XAML中的Metro应用)? - How to serialize and deserliaze a Dictionary<string,object> in WinRT (Metro app in C# and XAML)? C#XAML-Windows 8.1应用程序多语言资源问题AppBar - C# XAML - Windows 8.1 App Multilingual Resources Issue AppBar XAML和C#WebView在Windows Phone 8.1 App中不起作用 - XAML and C# WebView not working in Windows Phone 8.1 App 使用 xaml 和 c# 在 Windows Phone 8.1 中制作歌曲列表 - make songs list in Windows Phone 8.1 with xaml and c# C#Xaml(Windows Universal App)打开新页面 - C# Xaml (Windows Universal App) open new Page 如何在XAML / C#上虚拟化“像数据网格一样”水平和垂直控制(Windows 8.1 - WinRT) - How can I virtualize a “datagrid like” Control Horizontally and Vertically on XAML/C# (Windows 8.1 - WinRT)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM