2010-07-06 69 views
3

我創建了一個用作按鈕的自定義控件,但它具有屬性以指定多邊形(要在按鈕內繪製)的點以及兩個漸變色。我已經在代碼中聲明瞭所有的屬性,然後在XAML中編寫了模板,但它似乎沒有工作。如果我將這些值硬編碼到XAML中,它工作得很好,但如果我通過TemplateBinding使用屬性值,似乎沒有任何事情發生。任何想法如何讓這個工作?如何在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="#CCCCCC" GradientEnd="#777777" /> 
    </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(Point[]), 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 Point[] PlayPoints 
     { 
      get //hardcoded return at the moment to get it to work, but this will change later 
      { 
       System.Collections.Generic.List<Point> result = new System.Collections.Generic.List<Point>(); 

       double left = this.Width/2.77; 
       double top = this.Height/4.17; 
       double right = this.Width/1.43; 
       double middle = this.Height/2.0; 
       double bottom = this.Height/1.32; 

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

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

回答

3

Polygon.PointsPointCollection類型。我不相信Point[]適合這種類型。您需要更改PlayPoints的類型或使用IValueConverter進行類型更改。

+0

謝謝。這對多邊形的問題進行了排序。現在繪圖正確。我的顏色仍然是一個問題,雖然... – 2010-07-06 20:32:08

3

嘗試使用這個,而不是你的顏色綁定:

Color="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=GradientStart}" 

TemplateBinding只能在聲明是在ControlTemplate中,這是不是你一個LinearGradientBrush的情況下的可視化樹的內容有限的情況下。您可以在ControlTemplate中的Trigger Setters中看到相同的行爲,並使用相同的修補程序。

+1

對不起,約翰。我嘗試了這些變化無濟於事。沒有例外,但橢圓上的漸變沒有變化。 – 2010-07-06 22:31:24

+1

你看到你的橢圓是什麼?你是否在輸出窗口中出現錯誤? – 2010-07-06 22:59:30

+2

沒有錯誤,橢圓的背景只是純白的。沒有顏色也沒有梯度。它看起來完全一樣,如果我根本不應用背景。 – 2010-07-07 17:17:18

相關問題