【问题标题】:Preventing deformation in XAML Grid's background when resizing调整大小时防止 XAML Grid 的背景变形
【发布时间】:2010-11-05 01:00:46
【问题描述】:

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

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

我只需要能够在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>

【问题讨论】:

    标签: wpf xaml brushes


    【解决方案1】:

    我会做两个画笔,一个固定在右边,一个固定在左边。像这样的:

    <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" />
    ...
    

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

    【讨论】:

    • 形状位于网格背景中很重要。我想对 Grid 本身进行的唯一更改是添加 Background 属性。
    猜你喜欢
    • 2012-05-05
    • 1970-01-01
    • 2023-01-30
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 2017-03-29
    • 2014-09-06
    相关资源
    最近更新 更多