3

I have a control bind to an index property of an object which implements INotifyPropertyChanged.

The problem is, I don't know how to notify the property changed signal for that particular index string.

I was told that I can use OnPropertyChanged("") to notify the whole object need to be changed.

But what I need is something like OnPropertyChanged("Some index property string").

Is there anyway to do it?

Many thanks.

ps:

What I am trying to do is apply MVVM pattern. I use a viewmodel class to wrap a normal POCO object. So when I bind, I bind to the [index property], so that I can notify changed. This method saves me from:

  1. wrap the inner domain POCO object for EVERY property I need.
  2. notify property changed in every wrapped property.

CODE

public class ViewModelEx<T_Self, T_Core> : ViewModelEx<T_Self> where T_Self : ViewModelEx<T_Self, T_Core>
{
private static Type _s_coreType = typeof(T_Core);
private static Dictionary<string, PropertyInfo> _s_corePropInfos = new Dictionary<string, PropertyInfo>();

private static PropertyInfo GetPropertyInfo(string prop)
{
    if (_s_corePropInfos.ContainsKey(prop) == false)
        _s_corePropInfos.Add(prop, _s_coreType.GetProperty(prop));

    return _s_corePropInfos[prop];
}

public T_Core Core { get; set; }

public object this[string propName]
{
    get
    {
        return GetPropertyInfo(propName).GetValue(Core, null);
    }
    set
    {
        GetPropertyInfo(propName).SetValue(Core, value, null);
        IsModified = true;
        //RaisePropertyChanged(propName);
        RaisePropertyChanged("");
    }
}

public R Val<R>(Expression<Func<T_Core, R>> expr)
{
    return (R)this[Core.GetPropertyStr(expr)];
}

public void Val<R>(Expression<Func<T_Core, R>> expr, R val)
{
    this[Core.GetPropertyStr(expr)] = val;
}
H.B.
  • 166,899
  • 29
  • 327
  • 400
GaryX
  • 737
  • 1
  • 5
  • 20
  • Do you mean this[string] or this[int]? – benPearce Nov 08 '10 at 08:30
  • I mean this[string]. What is the difference regarding databinding/notify change between this[string] and this[int]? – GaryX Nov 08 '10 at 22:48
  • I eventually add property wrappers for each of the domain model proeprty. It turns out that there's not too much work. And it avoids all the problems. Anyway, it is an onceoff effort. – GaryX Nov 11 '10 at 22:38

1 Answers1

3

You cannot create notifications for specific index bindings in WPF, you can only notify all index-bindings:

RaisePropertyChanged(Binding.IndexerName);

Which should be the same as:

RaisePropertyChanged("Item[]");

You could override this string using the IndexerNameAttribute.

(In Silverlight you can actually specify an index inside the brackets to only affect that specific binding.)

H.B.
  • 166,899
  • 29
  • 327
  • 400