简体   繁体   English

从绑定传递的值作为字典中的键

[英]Passed value from binding as key in dictionary

I tried to make a ListView of ListViews using dictionary in xaml, but i cant figure out how to. 我试图使用xaml中的字典制作ListViews的ListView,但是我不知道该怎么做。

I have a dictionary: 我有一本字典:

   public Dictionary<string, float> dictionary {get;set;}

And

    ObservableCollection<string> Parameters{get; set;}

containing names of key values in said dictionary. 在所述字典中包含键值的名称。

I have a ListView with 我有一个ListView

    itemsSource = "{Binding Parameters}"

And an ListViews as DataTemplate with itemsSource that would be like: 还有一个ListViews作为带有itemsSource的DataTemplate ,就像这样:

    itemsSource= "{Binding dictionary[Passed_Value]}"

I cant manualy create ListViews with only selected values of Dictionary as user may choose which to display, and there are 10s of them. 我无法手动创建仅具有选定词典值的ListViews,因为用户可以选择要显示的值,并且其中有10s。

Or maybe you just wonder how to access a dictionary using Binding. 也许您只是想知道如何使用Binding访问字典。

Using this code: 使用此代码:

public Dictionary<string, double> Items
{
    get
    {
        var d = new Dictionary<string, double>();
        foreach (var item in Enumerable.Range(1, 10))
        {
            d.Add($"Key{item}", item);
        }
        return d;
    }
}

And this XAML: 而这个XAML:

<ListView ItemsSource="{x:Bind Items}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock>
                <Run Text="{Binding Key}" />
                <Run Text="=" />
                <Run Text="{Binding Value}" />
            </TextBlock>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

You will notice that I am not really accessing the dictionary using the key. 您会注意到,我并不是真正使用该键访问字典。 There is no way to access a dictionary by key when the key is only known through binding. 仅通过绑定才能知道键时,无法通过键访问字典。 That is to say, you cannot pass a binding to a binding. 也就是说,您不能将绑定传递给绑定。

Here's what you can do: 您可以执行以下操作:

<StackPanel DataContext="{Binding ElementName=ThisPage}">
    <TextBlock Text="{Binding Items[Key1]}" />
</StackPanel>

But, of course, this requires you know the key in advance and not bind to it. 但是,当然,这需要您事先知道密钥,而不是绑定到它。 There is no multi-binding in WinRT-XAML. WinRT-XAML中没有多重绑定。

I hope this clears up the confusion. 我希望这可以消除混乱。

Your question is quite a challenge to understand. 您的问题很难理解。 So, I thought I would just demonstrate creating a list of lists. 因此,我认为我只是演示如何创建列表列表。 Starting with this code-behind. 从这段代码开始。

public class Level1
{
    public int Key { get; set; }
    public string Name { get; set; }
    public IEnumerable<Level2> Items { get; set; }
}

public class Level2
{
    public int Key { get; set; }
    public string Name { get; set; }
}

public IEnumerable<Level1> Items
{
    get
    {
        return Enumerable.Range(1, 10)
            .Select(x => new Level1
            {
                Key = x,
                Name = $"Level 1 Item {x}",
                Items = Enumerable.Range(1, 10)
                    .Select(y => new Level2
                    {
                        Key = y,
                        Name = $"Level 2 Item {y}",
                    })

            });
    }
}

And this XAML: 而这个XAML:

<ListView ItemsSource="{x:Bind Items}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Name}" />
                <ItemsControl ItemsSource="{Binding Items}" Margin="12,0,0,0">
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBlock Text="{Binding Name}" />
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Looks like this: 看起来像这样:

在此处输入图片说明

Please note that I changed the nested ListView and made it an ItemsCOntrol because the nesting controls that each have a SELECTED behavior can cause you a real nightmare. 请注意,我更改了嵌套的ListView并将其设置为ItemsCOntrol,因为每个嵌套控件都具有SELECTED行为,这可能会引起您的噩梦。 Other than that, this does it. 除此之外,它做到了。 Not sure you need a Dictionary at all, but binding to a dictionary is certainly possible - just a little clunky. 不确定您根本不需要字典,但绑定字典肯定是可能的-有点笨拙。

Best of luck. 祝你好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM