Databinding to index property
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(""开发者_如何学C) 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:
- wrap the inner domain POCO object for EVERY property I need.
- 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;
}
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.)
精彩评论