public void Attach(NodeItem nodeItem) { myAttachedView = nodeItem; myAttachedView.IsFilteredOut = IsFilteredOut; var expr = myAttachedView.GetBindingExpression(TreeViewItem.IsExpandedProperty); if (expr != null) { // property bound to INode impl --> this is the master IsExpanded = myAttachedView.IsExpanded; } else { // no binding --> we are the master myAttachedView.IsExpanded = IsExpanded; } }
private bool SetViewProperty <T>(T value, [CallerMemberName] string propertyName = null) { if (myAttachedView == null) { return(false); } var dependencyPropertyField = myAttachedView.GetType() .GetField(propertyName + "Property", BindingFlags.Static | BindingFlags.Public | BindingFlags.FlattenHierarchy); if (dependencyPropertyField != null) { var expr = myAttachedView.GetBindingExpression((DependencyProperty)dependencyPropertyField.GetValue(myAttachedView)); if (expr != null) { // If this is a DependencyProperty with binding we have to update the source instead of setting the // DependencyProperty because setting the DependencyProperty directly will kill the binding var prop = expr.ResolvedSource.GetType().GetProperty(expr.ResolvedSourcePropertyName); if (!object.Equals(prop.GetValue(expr.ResolvedSource), value)) { prop.SetValue(expr.ResolvedSource, value); expr.UpdateTarget(); return(true); } return(false); } } { var prop = myAttachedView.GetType().GetProperty(propertyName); if (!object.Equals(prop.GetValue(myAttachedView), value)) { prop.SetValue(myAttachedView, value); return(true); } } return(false); }