/// <summary> /// make sure the DepObj object has a DepObjInfo object. /// Store the DepObjInfo object itself as an associated property of the /// DependencyObject. /// </summary> /// <param name="DepObj"></param> /// <returns></returns> public static DepObjInfo AssureObjInfo(DependencyObject DepObj) { // make sure the DepObjInfo object exists. Store in ObjInfo property. var objInfo = (DepObjInfo)DepObj.GetValue(ObjInfoProperty); if (objInfo == null) { objInfo = new DepObjInfo() { DepObj = DepObj }; DepObj.SetValue(ObjInfoProperty, objInfo); } return(objInfo); }
/// <summary> /// Handles changes to the Command property. /// </summary> private static void OnCommandChanged( DependencyObject d, DependencyPropertyChangedEventArgs e) { var newCmd = (ICommand)e.NewValue; var uiElem = d as UIElement; if (uiElem != null) { // make sure the DepObjInfo object exists. Store in ObjInfo property. var objInfo = DepObjInfo.AssureObjInfo(d); // hook or unhook the MouseLeave event on this UIElement. // store the event handler in te DepObjInfo object that stored the DepObj itself. if (newCmd == null) { uiElem.MouseLeave -= objInfo.MouseLeave; } else { uiElem.MouseLeave += objInfo.MouseLeave; } } }
/// <summary> /// Sets the Command property. /// </summary> public static void SetObjInfo(DependencyObject d, DepObjInfo value) { d.SetValue(ObjInfoProperty, value); }