如何在WPF中调整多边形大小?

3
我是你的助手,很高兴为您提供帮助。以下是您需要翻译的内容:

我正在开发我的第一个WPF应用程序,并测试一个自定义控件,它基本上是一个圆形,中间有一个播放按钮。但是似乎遇到了一点问题。当我绘制播放按钮时,无法随着圆的大小调整而调整。具体来说,当我将圆调整为更宽或更高时,播放按钮多边形保持相同的大小和绝对位置。请问有何建议可以通过设置XAML或代码来解决这个问题?

现有XAML:

<Window x:Class="WPFTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WPFTest">
    <StackPanel>
        <StackPanel.Resources>
            <Style TargetType="my:GradientButton">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type my:GradientButton}">
                            <Grid>
                                <Ellipse Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Stroke="{TemplateBinding Foreground}" VerticalAlignment="Top" HorizontalAlignment="Left">
                                    <Ellipse.Fill>
                                        <LinearGradientBrush>
                                            <GradientStop Color="{TemplateBinding GradientStart}" Offset="0"></GradientStop>
                                            <GradientStop Color="{TemplateBinding GradientEnd}" Offset="1"></GradientStop>
                                        </LinearGradientBrush>
                                    </Ellipse.Fill>
                                </Ellipse>
                                <Polygon Points="{TemplateBinding PlayPoints}" Fill="{TemplateBinding Foreground}" />
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </StackPanel.Resources>
        <my:GradientButton Content="Button" Height="50" x:Name="gradientButton1" Width="50" GradientStart="#FFCCCCCC" GradientEnd="#FFAAAAAA" PlayPoints="18,12 35,25 18,38" />
    </StackPanel>
</Window>

代码:

public class GradientButton : Button
    {
        internal static DependencyProperty GradientStartProperty;
        internal static DependencyProperty GradientEndProperty;
        internal static DependencyProperty PlayPointsProperty;

        static GradientButton()
        {
            GradientStartProperty = DependencyProperty.Register("GradientStart", typeof(Color), typeof(GradientButton));
            GradientEndProperty = DependencyProperty.Register("GradientEnd", typeof(Color), typeof(GradientButton));
            PlayPointsProperty = DependencyProperty.Register("PlayPoints", typeof(PointCollection), typeof(GradientButton));
        }

        public Color GradientStart
        {
            get { return (Color)base.GetValue(GradientStartProperty); }
            set { SetValue(GradientStartProperty, value); }
        }

        public Color GradientEnd
        {
            get { return (Color)base.GetValue(GradientEndProperty); }
            set { SetValue(GradientEndProperty, value); }
        }

        public PointCollection PlayPoints
        {
            get
            {
                //this is where I'm trying to make the resizing dynamic, but this property never seems to get hit when I put in a breakpoint?
                PointCollection result = new PointCollection();
                double top = this.Width / 2.778;
                double left = this.Width / 4.167;
                double middle = this.Height / 2.00;
                double right = this.Width / 1.429;
                double bottom = this.Height / 1.316;

                result.Add(new Point(left, top));
                result.Add(new Point(right, middle));
                result.Add(new Point(left, bottom));

                return result;
            }
            set { SetValue(PlayPointsProperty, value); }
        }
    }

1
如果你只是想要一个显示一些花哨图形的按钮,比如一个充满彩虹色的圆圈中的箭头,那么你不需要创建自己的按钮类。只有在添加一些新的行为,比如三次点击等时才需要这样做。最好只是为此创建一个样式并使用常规按钮控件。 - Wallstreet Programmer
1个回答

12
您需要将多边形的Stretch属性设置为Uniform。

谢谢。这似乎已经帮我解决了部分问题。现在我面临的问题是,我的多边形(形状类似播放按钮)正在延伸到椭圆的边缘之外。我怀疑解决这个问题的方法是创建一个容器,将我的多边形放在其中?如果是这样,那么我又回到了起点,因为我需要容器动态调整大小,以便在我的椭圆内存在(作为正方形/矩形)... - Sonny Boy
只需将多边形的Margin设置一下,使其与外边缘有一些间距。如果您需要更动态地进行间距设置,请在网格中添加3个行和列定义,并使用*大小来获得多边形周围的比例间距。 - John Bowen
谢谢,约翰。我将尝试在网格中使用行/列的想法。 - Sonny Boy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接