【问题标题】:How to create WPF Custom Control without ControlTemplate?如何在没有 ControlTemplate 的情况下创建 WPF 自定义控件?
【发布时间】:2014-12-11 06:24:09
【问题描述】:

我需要编写一个 WPF 自定义控件,它由 4 个其他 FrameworkElement 派生的自定义类组成。通常我会使用 Grid 来排列它们,但是 Grid 有一些布局问题,我必须编写自己的继承自 Panel 的类。当然,我的自定义控件可以从 Panel 继承,但会暴露 Children 和其他 Panel 功能,这是不应该的。

我需要一个自定义控件,它只显示我添加的一些属性和典型的 FrameworkElement 属性。我正在考虑我的自定义控件继承自 Control。但我不想使用 ControlTemplates。我想创建面板并从后面的代码中添加 FrameworkElements。这是怎么做到的?

让我总结一下问题: 1) Control 是最好的派生类吗?

2) 如何在不使用 ControlTemplate 的情况下将 FrameworkElements 添加到 Control 派生类?

【问题讨论】:

  • 你考虑过 UserControl 基类吗?这将允许您在 XAML 或代码中设计您的控件,并仅公开您希望在外部可见的属性。
  • @kidshaw:我宁愿不使用 XAML,因为它会使一切变得不必要的复杂和缓慢。无论如何,我需要编写自己的 Panel 类,即使在使用 UserControl 时也是如此,因为没有一个 Panel 支持我需要的布局。从 Control 继承时,我可以在 MeasureOverride 和 ArrangeOverride 中进行布局,这意味着我可以在 1 个类中完成我需要的一切。
  • 不确定我是否同意 XAML 让它变得不必要地复杂,但作为一种观点可以理解 :) - 它让复杂变得简单,让简单变得复杂。

标签: wpf custom-controls


【解决方案1】:
  1. 是的。控制是最好的派生类。

  2. 您需要重写方法 GetVisualChild() 和属性 VisualChildrenCount。我正在考虑您希望有 4 个自定义框架元素作为您的孩子,并在构造函数中初始化该集合。然后代码如下,

    private List<UIElement> visualChildren;
    
    protected override int VisualChildrenCount
    {
        get
        {
            return this.visualChildren.Count;
        }
    }
    
    protected override Visual GetVisualChild(int index)
    {
        return this.visualChildren[index];
    }
    

您也可以重写 MeasureOverride 和 ArrangeOverride 方法来为子级分配大小并分别排列子级。

More information

【讨论】:

  • 您还需要为每个孩子调用 AddVisualChild。考虑使用 VisualCollection 而不是 List。
【解决方案2】:

这是根据 XAML Lover 和 Marat Khasanov 的建议解决我的问题的实现。 TestCustomControl 将 2 个文本框彼此对角放置。右上角的使用尽可能多的宽度和尽可能高的高度。左下方的那个使用尽可能多的高度和尽可能多的宽度。

+------------+--------+
|            |        |
|            |TextBox1|
|            |        |
+------------+--------+
|  TextBox2  |        |
+------------+--------+

这种排列方式对于 Grid 来说是个问题,因为要测量一个 TextBox 的大小,它需要知道另一个 TextBox 的大小,而这是不可能的。 Grid 通过使用无限空间测量第一个 TextBox,然后使用第一个文本框的大小测量另一个文本框,然后使用第二个文本框的大小再次测量第一个文本框来解决这个难题。这会导致各种问题,而 TestCustomControl 完全可以避免这些问题。

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;


namespace TestControl {


  public class TestCustomControl: Control  {

    VisualCollection visualCollection;
    Rectangle RectangleToLeft;
    TextBox TextBoxTopRight;
    TextBox TextBoxBottomLeft;
    Rectangle RectangleBottomRight;


    public TestCustomControl() {
      RectangleToLeft      = new Rectangle { Fill = Brushes.LightYellow };
      TextBoxTopRight      = new TextBox {Text = "TR", FontSize = 22, HorizontalAlignment=HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Stretch };
      TextBoxBottomLeft    = new TextBox {Text = "BL", FontSize = 22, HorizontalAlignment=HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Center };
      RectangleBottomRight = new Rectangle {Fill = Brushes.LightBlue };

      //new UIElementCollection(this, logicalParent);
      visualCollection = new VisualCollection(this);
      visualCollection.Add(RectangleToLeft);
      visualCollection.Add(TextBoxTopRight);
      visualCollection.Add(TextBoxBottomLeft);
      visualCollection.Add(RectangleBottomRight);
    }


    protected override int VisualChildrenCount {
      get {
        return visualCollection.Count;
      }
    }


    protected override System.Windows.Media.Visual GetVisualChild(int index) {
      return visualCollection[index];
    }


    protected override System.Windows.Size MeasureOverride(System.Windows.Size constraint) {
      TextBoxTopRight.Measure(constraint);
      TextBoxBottomLeft.Measure(constraint);
      Size returnedSize = constraint;
      if (double.IsInfinity(constraint.Height)) {
        returnedSize.Height = TextBoxTopRight.DesiredSize.Height + TextBoxBottomLeft.DesiredSize.Height;
      }
      if (double.IsInfinity(constraint.Width)) {
        returnedSize.Width = TextBoxTopRight.DesiredSize.Width + TextBoxBottomLeft.DesiredSize.Width;
      }
      return constraint;
    }


    protected override System.Windows.Size ArrangeOverride(System.Windows.Size arrangeBounds) {
      double remainingWidth = Math.Max(0, arrangeBounds.Width - TextBoxTopRight.DesiredSize.Width);
      double remainingHeight = Math.Max(0, arrangeBounds.Height - TextBoxBottomLeft.DesiredSize.Height);
      RectangleToLeft.Arrange(new Rect(0, 0, remainingWidth, remainingHeight));
      TextBoxTopRight.Arrange(new Rect(remainingWidth, 0, TextBoxTopRight.DesiredSize.Width, remainingHeight));
      TextBoxBottomLeft.Arrange(new Rect(0, remainingHeight, remainingWidth, TextBoxBottomLeft.DesiredSize.Height));
      RectangleBottomRight.Arrange(new Rect(remainingWidth, remainingHeight, TextBoxTopRight.DesiredSize.Width, TextBoxTopRight.DesiredSize.Height));
      return arrangeBounds;
    }


    protected override Geometry GetLayoutClip(Size layoutSlotSize) {
      if (ClipToBounds)
        return new RectangleGeometry(new Rect(RenderSize));
      else
        return null;
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多