自定义面板无法在XAML Designer中正确呈现

时间:2017-11-24 07:57:42

标签: wpf xaml designer

我制作了一个包含Expander的自定义面板,它有一个Stackpanel,可以在其中直接分配给孩子们,而不是在Panel's Children中。但是,设计师拒绝渲染任何直接儿童(Expander)儿童。

是否需要做其他事情,使设计师的布局无效,注意到那里有东西?

public class ExpanderPanel : Panel
{
    const double leftMargin = 24.0;

    private Expander _expander;

    public string Header
    {
        get { return (string)GetValue(HeaderProperty); }
        set { SetValue(HeaderProperty, value); }
    }

    public static readonly DependencyProperty HeaderProperty =
        DependencyProperty.Register("Header", typeof(string), typeof(ExpanderPanel), new FrameworkPropertyMetadata(string.Empty, (d, e) =>
        {
            var panel = d as ExpanderPanel;
            panel._expander.Header = panel.Header;
        }));

    public bool IsExpanded
    {
        get { return (bool)GetValue(IsExpandedProperty); }
        set { SetValue(IsExpandedProperty, value); }
    }

    public static readonly DependencyProperty IsExpandedProperty =
        DependencyProperty.Register("IsExpanded", typeof(bool), typeof(ExpanderPanel), new FrameworkPropertyMetadata(true, (d, e) =>
        {
            var panel = d as ExpanderPanel;
            panel._expander.IsExpanded = panel.IsExpanded;
        }));

    public ExpanderPanel()
    {
        _expander = new Expander()
        {
            Content = new StackPanel()
            {
                HorizontalAlignment = HorizontalAlignment.Stretch,
                Margin = new Thickness(leftMargin - 2, 0, 0, 0) // The Margin - 2 for borders... Don't know a better way...
            },
            HorizontalAlignment = HorizontalAlignment.Stretch,
        };

        Children.Add(_expander);
    }

    private void InitializePanel()
    {
        _expander.Header = Header;
        _expander.Width = Width;
        _expander.IsExpanded = IsExpanded;

        var sp = _expander.Content as StackPanel;

        while (InternalChildren.Count > 1)
        {
            var child = InternalChildren[1];
            InternalChildren.RemoveAt(1);
            sp.Children.Add(child);
        }
    }

    public override void EndInit()
    {
        base.EndInit();
        InitializePanel();
    }

    protected override Size MeasureOverride(Size availableSize)
    {
        var size = new Size();

        _expander.Width = availableSize.Width;

        _expander.Measure(availableSize);
        size.Width = leftMargin + _expander.DesiredSize.Width;
        if (size.Width > availableSize.Width)
        {
            size.Width = availableSize.Width;
        }

        size.Height += _expander.DesiredSize.Height;

        return size;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        var location = new Point();

        _expander.Arrange(new Rect(location, _expander.DesiredSize));

        return finalSize;
    }
}

1 个答案:

答案 0 :(得分:2)

最后通过从类中记录来找到答案:在通过设计器运行时,永远不会调用构造函数。因此,不会创建Expander,我会假设发生一些NullReferenceExceptions。

我不明白为什么构造函数不会被调用。修复了将构造函数中的逻辑放入由事件处理程序调用的Initialize方法中。