如何绑定(静态)字典标签? [英] How to bind a (static) Dictionary to Labels?

查看:87
本文介绍了如何绑定(静态)字典标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态字典

class X { static Dictionary<string,string> MyDict {get { ... }} }

本字典包含数据我想在一个网格控件显示:

This Dictionary contains Data i want to show in a Grid-Control:

<Grid>
  <!-- Row and Column-Definitions here -->
  <Label Grid.Row="0" Grid.Column="0" Content="{Binding MyDict.Key=="foo" }" ToolTip="foo" />
  <!-- some more labels -->
</Grid>

1。)我不知道如何访问(在XAML)到字典中

1.) i dont know how to get access (in xaml) to the dictionary

2。)我​​想指定的关键标签的内容属性的值绑定。

2.) i want to bind the Value of a specified key to the Content-Property of the Label.

如何做到这一点?

推荐答案

您需要使用的转换器这将允许您通过词典的> ConverterParameter 。

You need to use a converter which will allow you to extract your value out of the Dictionary via the ConverterParameter.

public class DictConverter: IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Dictionary<string,string> data = (Dictionary<string,string>)value;
        String parameter = (String)parameter;
        return data[parameter];
    }
}

该XAML将如下所示...

The XAML would be as follows...

<Window.Resources>
    <converters:DictConverter x:Key="MyDictConverter"/>
</Window.Resources>

Content="{Binding MyDictProperty, Converter={StaticResource MyDictConverter}, ConverterParameter=foo}"

这篇关于如何绑定(静态)字典标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆