调整大小时防止 XAML 网格背景变形

发布于 2024-07-25 03:44:50 字数 1528 浏览 3 评论 0原文

我正在用 XAML 编写一个画笔,可以用它来绘制 Grid 的背景来创建横幅。 它看起来像这样:

应用于网格背景的画笔示例

我希望当 Window 调整大小时画笔与 Grid 一起“拉伸”,但我不希望中心角变形。

变形的背景画笔

我只需要能够在网格背景中绘制形状。 怎样才能避免变形呢?

我写的代码如下所示:

<Window x:Class="WpfApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="60" Width="300">
    <Window.Resources>
        <DrawingBrush x:Key="GridBackground">
            <DrawingBrush.Drawing>
                <DrawingGroup>
                    <DrawingGroup.Children>
                        <GeometryDrawing Geometry="M0,1 0,0 0.4,0 0.45,0.5 0.4,1Z" Brush="#FF6A00" />
                        <GeometryDrawing Geometry="M0.6,1 0.55,0.5 0.6,0 1,0 1,1Z" Brush="#FF0000" />
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Window.Resources>
    <Grid Background="{StaticResource GridBackground}">
        <TextBlock Foreground="White" VerticalAlignment="Center">Some text</TextBlock>
    </Grid>
</Window>

I am writing a brush in XAML that I can use to paint the background of a Grid to create a banner. It looks like this:

An example of the brush applied to the background of a Grid

I want the brush to "stretch" with the Grid when the Window resizes, but I do not want the center angles to deform.

The deformed background brush

I only need to be able to draw the shapes in the background of a Grid. How can I avoid the deformation?

The code I've written looks like this:

<Window x:Class="WpfApplication.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Height="60" Width="300">
    <Window.Resources>
        <DrawingBrush x:Key="GridBackground">
            <DrawingBrush.Drawing>
                <DrawingGroup>
                    <DrawingGroup.Children>
                        <GeometryDrawing Geometry="M0,1 0,0 0.4,0 0.45,0.5 0.4,1Z" Brush="#FF6A00" />
                        <GeometryDrawing Geometry="M0.6,1 0.55,0.5 0.6,0 1,0 1,1Z" Brush="#FF0000" />
                    </DrawingGroup.Children>
                </DrawingGroup>
            </DrawingBrush.Drawing>
        </DrawingBrush>
    </Window.Resources>
    <Grid Background="{StaticResource GridBackground}">
        <TextBlock Foreground="White" VerticalAlignment="Center">Some text</TextBlock>
    </Grid>
</Window>

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

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

发布评论

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

评论(1

少年亿悲伤 2024-08-01 03:44:50

我会制作两把画笔,一支固定在右侧,一支固定在左侧。 像这样的事情:

<Grid>
     <GeometryXXX Geometry="M0,1 0,0 0.4,0 0.45,0.5 0.4,1Z" Width="300" HorizontalAlignment="Left" Brush="#FF6A00">
     <GeometryXXX Geometry="M0,1 0,0 0.4,0 0.45,0.5 0.4,1Z" Width="300" HorizontalAlignment="Right" Brush="#FF0000">
     <TextBlock Foreground="White" VerticalAlignment="Center">Some text</TextBlock>
</Grid>

我没有打开编译器,并且我不记得几何图形绘制对象的名称。

另一种方法是创建一个值转换器,并执行类似的操作:

...
    <GeometryDrawing Geometry="{Binding Width, ValueConverter=LeftAngledThing}" Brush="#FF6A00" />
    <GeometryDrawing Geometry="{Binding Width, ValueConverter=LeftAngledThing}" Brush="#FF0000" />
...

您需要查找如何执行此操作的确切语法,因为我现在不记得了。

I would make it two brushes, one anchored to the right, and one anchored to the left. Something like this:

<Grid>
     <GeometryXXX Geometry="M0,1 0,0 0.4,0 0.45,0.5 0.4,1Z" Width="300" HorizontalAlignment="Left" Brush="#FF6A00">
     <GeometryXXX Geometry="M0,1 0,0 0.4,0 0.45,0.5 0.4,1Z" Width="300" HorizontalAlignment="Right" Brush="#FF0000">
     <TextBlock Foreground="White" VerticalAlignment="Center">Some text</TextBlock>
</Grid>

I don't have my compiler open, and I don't remember the name of the Geometry drawing object.

The other way of doing it would be to create a valueconverter, and do something like:

...
    <GeometryDrawing Geometry="{Binding Width, ValueConverter=LeftAngledThing}" Brush="#FF6A00" />
    <GeometryDrawing Geometry="{Binding Width, ValueConverter=LeftAngledThing}" Brush="#FF0000" />
...

You would need to look up the exact syntax for how to do this though, as I don't remember it right now.

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