2010-07-06 66 views
1

我正在开发我的第一个WPF应用程序,并且正在测试一个自定义控件,该控件对于在其中间放置一个播放按钮的圆圈很重要。我似乎碰到了一些麻烦。当我画出我的播放按钮时,我似乎无法使其与圆圈一起调整大小。具体来说,当我将圆圈的大小调整为更大或更大时,播放按钮多边形保持相同的大小和相同的绝对位置。任何设置我的XAML或代码纠正这个指针?如何在WPF中调整多边形的大小?

现有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); } 
     } 
    } 
+0

,就像充满彩虹色的圆圈中的箭头一样,你不需要创建自己的按钮类。只有在添加一些新行为时才能做到这一点,例如tripple点击。您更适合为此创建样式并使用常规按钮控件。 – 2010-07-07 13:40:31

回答

5

您需要将多边形的拉伸性。如果你想要的是显示一些花哨的图形按钮设置为统一

+0

谢谢。这似乎让我成为那里的一部分。我现在面临的问题是,我的多边形(形状像一个播放按钮)延伸到我的椭圆的边缘。我怀疑解决这个问题的方法是创建一个容器,里面有我的多边形?如果是这样,看起来我回到了原点,因为我需要动态调整大小以使其在椭圆内存在(作为正方形/矩形)... – 2010-07-06 21:37:19

+0

只需在多边形上设置边距以使其与外边缘,或者如果您需要更多动态空间,则可以使用*尺寸将3 Row和ColumnDefinitions添加到网格,以获得Polygon周围的比例间距。 – 2010-07-07 02:20:14

+0

谢谢,约翰。我会尝试网格的行/列的想法。 – 2010-07-07 17:18:12