【问题标题】:How to resize a polygon in WPF?如何在 WPF 中调整多边形的大小?
【发布时间】:2010-07-07 05:30:28
【问题描述】:

我正在开发我的第一个 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); }
        }
    }

【问题讨论】:

  • 如果你想要的只是一个显示一些精美图形的按钮,比如一个充满彩虹色的圆圈中的箭头,你不需要创建自己的按钮类。仅当您添加一些新行为时才执行此操作,例如三次单击。您最好为此创建一个样式并使用常规按钮控件。

标签: c# .net wpf xaml


【解决方案1】:

您需要将 Polygon 的 Stretch 属性设置为 Uniform

【讨论】:

  • 谢谢。这似乎让我成为了其中的一部分。我现在面临的问题是我的多边形(形状像一个播放按钮)在我的椭圆边缘延伸。我怀疑解决这个问题的方法是创建一个容器,里面有我的多边形?如果是这样,我似乎又回到了第一个正方形,因为我需要动态调整容器大小以在我的椭圆内存在(作为正方形/矩形)......
  • 只需在多边形上设置边距,使其与外边缘有一些间距,或者如果您需要更动态地间隔,使用 * 大小将 3 行和列定义添加到网格中,以获得周围的比例间距多边形。
  • 谢谢,约翰。我会用网格尝试行/列的想法。
猜你喜欢
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-16
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
相关资源
最近更新 更多