wpf绑定问题,viewmodel一个字典属性里面的值如何在xaml页面中绑定? RRS feed

  • 问题

  • 如题,我想绑定:prodata.mfc["mfcname"].mfcvalue

    prodata是一个类,prodata.mfc是字典项,直接在xaml中写content={Binding Path=prodata.mfc['mfcname'].mfcvalue}貌似不行,


    2011年11月10日 2:17


  • Binding.Converter是可以解决你的问题,比如这样:

    public class DictionaryItemConverter : IValueConverter
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
                var dict = value as Dictionary<string, string>;
                if (dict != null)
                    return dict[parameter as string];
                throw new NotImplementedException();
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
                throw new NotImplementedException();
     <local:DictionaryItemConverter x:Name="DictConvert" />
    <TextBlock Text="{Binding Converter={StaticResource DictConvert}, ConverterParameter=test}" />

    Best regards,

    Sheldon _Xiao[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • 已标记为答案 a013strife 2011年11月11日 11:19
    2011年11月11日 8:13