Define a converter:
public class RowIndexConverter : IValueConverter { public object Convert( object value, Type targetType, object parameter, CultureInfo culture ) { var row = (IDictionary<string, object>) value; var key = (string) parameter; return row.Keys.Contains( key ) ? row[ key ] : null; } public object ConvertBack( object value, Type targetType, object parameter, CultureInfo culture ) { throw new NotImplementedException( ); } }
Bind to a custom dictionary definition. There are many overrides that I skipped, but the indexer is important because it emits an event of a changed property when the value changes. This is necessary to bind the source to the target.
public class BindableRow : INotifyPropertyChanged, IDictionary<string, object> { private Dictionary<string, object> _data = new Dictionary<string, object>( ); public object Dummy
Use your converter in your .xaml file. First contact him:
<UserControl.Resources> <ViewModelHelpers:RowIndexConverter x:Key="RowIndexConverter"/> </UserControl.Resources>
Then, for example, if your dictionary has an entry where the key is "Name", and then bind to it: use
<TextBlock Text="{Binding Dummy, Converter={StaticResource RowIndexConverter}, ConverterParameter=Name}">
Phillip Ngan Nov 10 '09 at 3:07 2009-11-10 03:07
source share