How to bind a (static) Dictionary to Labels?

后端 未结 4 1001
自闭症患者
自闭症患者 2021-01-19 05:57

I have a static Dictionary

class X { static Dictionary MyDict {get { ... }} }

This Dictionary contains Data i want to

4条回答
  •  借酒劲吻你
    2021-01-19 06:41

    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 data = (Dictionary)value;
            String parameter = (String)parameter;
            return data[parameter];
        }
    }
    

    The XAML would be as follows...

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

提交回复
热议问题