2009-02-02 3 views
0

ParentAdapter 구현을 작성하려고합니다. 필자가 작성한 일부 WPF 컨트롤에 디자인 타임 지원을 제공하는 데 관심이 있습니다.이 방법은 다른 컨테이너 컨트롤에 항목을 다시 부모 개체로 지정하는 사용자 지정 논리를 관리하는 방법입니다. 나는 StackPanel 생성 클래스의 개념으로 디자인 타임에 Button 요소 만 부모가 될 수있게했다. (네, 패널 자체가 이것을 지원하는 코드를 필요로한다는 것을 알고 있습니다.) 무엇부터 시작 했습니까?간단한 ParentAdapter 구현은 어떤 모양입니까?

using System; 
using System.Windows; 
using System.Windows.Controls; 
using Microsoft.Windows.Design.Interaction; 
using Microsoft.Windows.Design.Model; 

namespace ControlLibrary.Design 
{ 
    internal class SimplePanelParentAdapter : ParentAdapter 
    { 
     public override bool CanParent(ModelItem parent, Type childType) 
     { 
      return (childType == typeof(Button)); 
     } 

     // moves the child item into the target panel; in this case a SimplePanel 
     public override void Parent(ModelItem newParent, ModelItem child) 
     { 
      using (ModelEditingScope undoContext = newParent.BeginEdit()) 
      { 
       // is this correct? 
       //child.Content.SetValue("I'm in a custom panel!"); 
       SimplePanel pnl = newParent.GetCurrentValue() as SimplePanel; 
       pnl.Children.Add(child.GetCurrentValue() as UIElement);     
       undoContext.Complete(); 
      } 

     } 

     public override void RemoveParent(ModelItem currentParent, ModelItem newParent, ModelItem child) 
     { 
      // No special things need to be done, right? 
      child.Content.SetValue("I was in a custom panel."); 
     } 
    } 
} 

내 사용자 정의 패널을 통해 버튼을 드래그 나는 즉시, 디자인 타임에이 작업을하는 NullReferenceException은 VS 코드 내 깊은 곳에서 발생합니다 : 나는 ParentAdapter 간단한이 될 수있을 것이라고 생각 . 내 코드는 예외를 던지지 않고 있습니다. 왜냐하면 나는 내 방법을 통해 단계를 밟을 수 있기 때문입니다. 호출 스택은 Microsoft.Windows.Design.Developer.dll의 코드가 예외를 throw하고 있음을 나타냅니다.

분명히 내가 잘못한 일을하고 있지만 설명서에 예제가없고 내 search-fu는 아무도 시도하지 않았거나 시도하고있는 사람이 아무 것도 말하지 않는 것으로 나타납니다. 누구 제안이 있습니까?

답변

0

내 질문에 대한 답을 직접 찾았습니다. 문제는 ModelItem 랩퍼 대신 모델을 편집하여 발생합니다. 내가 할 (그리고 작업을 수행)해야하는 것은이 같은 것입니다 :

using System; 
using System.Windows.Controls; 
using Microsoft.Windows.Design.Interaction; 
using Microsoft.Windows.Design.Model; 

namespace ControlLibrary.Design 
{ 
    internal class SimplePanelParentAdapter : ParentAdapter 
    { 
     public override bool CanParent(ModelItem parent, Type childType) 
     { 
      return (childType == typeof(Button)); 
     } 

     // moves the child item into the target panel; in this case a SimplePanel 
     public override void Parent(ModelItem newParent, ModelItem child) 
     { 
      using (ModelEditingScope undoContext = newParent.BeginEdit()) 
      { 
       ModelProperty prop = newParent.Properties["Children"]; 
       ModelItemCollection items = (ModelItemCollection)prop.Value; 
       items.Add(child); 

       undoContext.Complete(); 
      } 

     } 

     public override void RemoveParent(ModelItem currentParent, ModelItem newParent, ModelItem child) 
     { 
      using (ModelEditingScope scope = child.BeginEdit()) 
      { 
       ModelProperty prop = currentParent.Properties["Children"]; 
       ((ModelItemCollection)prop.Value).Remove(child); 

       scope.Complete(); 
      } 
     } 
    } 
} 
내가 첫 번째 코드와 I는 어린이 재산() 추가 전화했는데 어떻게 확신을 쓸 때 나는 혼란스러워했다

; ModelProperty.Value가 ModelItemCollection으로 컬렉션을 래핑하는 것처럼 보이므로 클래스를 둔한 인터페이스를 사용하도록 만들지 않는 한 이렇게하면 작동하지 않을 것입니다.