C# 如何在WPF中调整多边形的大小?
我正在开发我的第一个WPF应用程序,我正在测试一个自定义控件,它是一个圆圈,中间画了一个播放按钮。不过,我似乎遇到了点麻烦。当我画我的播放按钮时,我似乎无法让它沿着圆圈调整大小。具体地说,当我将圆的大小调整为更宽或更高时,“播放按钮”多边形的大小和绝对位置保持不变。有没有关于设置XAML或代码以更正此问题的指针 现有XAML:C# 如何在WPF中调整多边形的大小?,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我正在开发我的第一个WPF应用程序,我正在测试一个自定义控件,它是一个圆圈,中间画了一个播放按钮。不过,我似乎遇到了点麻烦。当我画我的播放按钮时,我似乎无法让它沿着圆圈调整大小。具体地说,当我将圆的大小调整为更宽或更高时,“播放按钮”多边形的大小和绝对位置保持不变。有没有关于设置XAML或代码以更正此问题的指针 现有XAML: <Window x:Class="WPFTest.MainWindow" xmlns="http://schemas.microsoft.com/w
<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>
您需要将多边形的“拉伸”属性设置为“均匀”(Uniform),谢谢。这似乎让我走了一段路。我现在面临的问题是,我的多边形(形状像一个播放按钮)延伸到椭圆的边缘。我想解决这个问题的方法是创建一个某种容器,其中包含我的多边形?如果是这样的话,我似乎又回到了原点,因为我需要动态调整容器的大小以使其存在(作为正方形/矩形)在我的椭圆内…只需在多边形上设置边距,以使其与外边保持一定的间距,或者如果需要更动态地进行间距,请使用*大小向网格添加3行和列定义,以获得多边形周围的比例间距。谢谢,John。我将尝试网格中的行/列概念。如果您只需要一个按钮来显示一些奇特的图形,比如一个圆圈中的箭头,里面填充了一些彩虹颜色,那么您不需要创建自己的按钮类。仅当您添加一些新行为(例如tripple click)时才执行此操作。您最好只为此创建一个样式,并使用常规按钮控件。
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); }
}
}