Following you can find example how we can find binding depandancy propertyfor all child elements. I usually use it when I use explicit binding mode and when I need to commit elements data changies, I go elements one by oneand perform UpdateSource(). Here is the code taht can perform UpdateSource for all child elements of parent element (for example: DataGrid ):
public static void UpdateItemSource(FrameworkElement item)
{
FieldInfo[] infos = item.GetType().GetFields(BindingFlags.Public |
BindingFlags.FlattenHieratchy |
BindingFlags.InstacFlags.Static); foreach (FieldInfo field in infos) { if (field.FieldType == typeof(DependencyProperty)) { DependencyProperty dp = (DependencyProperty)field.GetValue(null);
BindingExpression ex = item.GetBindingExpression(dp); if (ex != null) { if(ex.ParentBinding.UpdateSourceTrigger == Update SourceTrigg er.Explicit)
ex.UpdateSource(); } } } int count = VisualTreeHelper.GetChildrenCount( item); for (int i = 0; i < count; i++) { FrameworkElement child = VisualTreeHelper.GetChild(item, i) as FrameworkElement;
if(child != null)
UpdateItemSource(child);
}
}
אין תגובות:
הוסף רשומת תגובה