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는 아무도 시도하지 않았거나 시도하고있는 사람이 아무 것도 말하지 않는 것으로 나타납니다. 누구 제안이 있습니까?