如何创建在某些地方延伸的 WPF 路径

发布于 2024-07-13 17:23:44 字数 565 浏览 7 评论 0原文

我想为窗口选项卡创建一个 WPF 控件,并且希望它具有特定的形状。 像这样的东西;

      +------------------------------+
      |                              |
*     |                              |
      |                              |
   +--+                              +--+
6  |                                    |  6
   +------------------------------------+   
     6       stretching section       6

所以左下角和右下角的小标签是固定大小的; 6x6,大约。 但现在我希望中心部分能够伸展到我将其放入的任何容器的宽度。

我目前正在使用 Path 对象,但我不知道如何获得拉伸部分,或者即使 Path 是正确的方法。

谁能建议创建这种半拉伸形状的最佳方法?

I'd like to create a WPF control for a window tab, and I want it to have a particular shape. Something like this;

      +------------------------------+
      |                              |
*     |                              |
      |                              |
   +--+                              +--+
6  |                                    |  6
   +------------------------------------+   
     6       stretching section       6

So the little tabs at the bottom left and bottom right are fixed size; 6x6, roughly. But now I want the centre section to stretch out to the width of whatever container I slap it into.

I'm using a Path object at the moment, but I can't figure out how to get a stretching section, or even if Path is the right way to go.

Can anyone suggest the best way to create this kind of half-stretchable shape?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮凉 2024-07-20 17:23:44

我通过创建一个继承自 StackPanel 的“StretchStackPanel”来完成应用程序中的拉伸部分。 该类如下所示:

public class StretchStackPanel : StackPanel
{
    public static DependencyProperty StretchDependencyProperty = DependencyProperty.Register("Stretch", typeof(StretchMode), typeof(StretchStackPanel));

    protected override Size MeasureOverride(Size availableSize)
    {
        var baseSize = base.MeasureOverride(availableSize);

        if (availableSize.Width != double.PositiveInfinity && (Stretch & StretchMode.Horizontal) == StretchMode.Horizontal )
        {
            baseSize.Width = availableSize.Width;    
        }
        if (availableSize.Height != double.PositiveInfinity && (Stretch & StretchMode.Vertical) == StretchMode.Vertical)
        {
            baseSize.Height = availableSize.Height;
        }

        return baseSize;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        var baseSize = base.ArrangeOverride(finalSize);

        if ((Stretch & StretchMode.Horizontal) == StretchMode.Horizontal )
        {
            baseSize.Width = finalSize.Width;    
        }

        if ((Stretch & StretchMode.Vertical) == StretchMode.Vertical)
        {
            baseSize.Height = finalSize.Height;
        }
        return baseSize;
    }

    [Category("Layout")]
    public StretchMode Stretch
    {
        get
        {
            return (StretchMode)GetValue(StretchDependencyProperty);
        }
        set
        {
            SetValue(StretchDependencyProperty, value);
        }
    }
}

您想要的是一个包含 2 行的网格。 顶行的内容应将其水平对齐设置为居中。 底行的内容应该是一个 StretchStackPanel。

I did the stretching part in my app by creating a "StretchStackPanel" that inherits from StackPanel. The class looks like this:

public class StretchStackPanel : StackPanel
{
    public static DependencyProperty StretchDependencyProperty = DependencyProperty.Register("Stretch", typeof(StretchMode), typeof(StretchStackPanel));

    protected override Size MeasureOverride(Size availableSize)
    {
        var baseSize = base.MeasureOverride(availableSize);

        if (availableSize.Width != double.PositiveInfinity && (Stretch & StretchMode.Horizontal) == StretchMode.Horizontal )
        {
            baseSize.Width = availableSize.Width;    
        }
        if (availableSize.Height != double.PositiveInfinity && (Stretch & StretchMode.Vertical) == StretchMode.Vertical)
        {
            baseSize.Height = availableSize.Height;
        }

        return baseSize;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        var baseSize = base.ArrangeOverride(finalSize);

        if ((Stretch & StretchMode.Horizontal) == StretchMode.Horizontal )
        {
            baseSize.Width = finalSize.Width;    
        }

        if ((Stretch & StretchMode.Vertical) == StretchMode.Vertical)
        {
            baseSize.Height = finalSize.Height;
        }
        return baseSize;
    }

    [Category("Layout")]
    public StretchMode Stretch
    {
        get
        {
            return (StretchMode)GetValue(StretchDependencyProperty);
        }
        set
        {
            SetValue(StretchDependencyProperty, value);
        }
    }
}

What you want is a Grid with 2 rows. The content of the top row should have it's horizontal alignment set to center. the content of the bottom row should be a StretchStackPanel.

心舞飞扬 2024-07-20 17:23:44

为什么不创建两个不同的小部件? 一个用于中心部分,另一个用于可拉伸部分。 然后将它们粘在一起放在另一个容器中,形成一个统一的控件。

Why not create two different widgets? One for the centre section and another for the stretchable section. Then stick them together in another container to form a unified control.

娇女薄笑 2024-07-20 17:23:44

我认为您应该重写控件中的 MeasureOverride 方法并获取内容的 DesiredSize (通过调用 Content/Children 上的 Measure 方法)。 然后你可以根据这个大小创建你的路径。

I think you should override the MeasureOverride method in your control and get DesiredSize of your content (by calling Measure method on Content/Children). Then you can create your Path based on this size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文