2013-04-20 94 views
1

我有一個非常簡單的基於按鈕的控件,它顯示一個橢圓,其顏色取自稱爲「Brush」的自定義依賴項控件。WPF自定義控件屬性設置器

該模板顯示具有適當顏色的橢圓,但觸發器中的Setters無法識別「Brush」屬性(在下面的XAML文件中突出顯示的錯誤)。

如何訪問setter中的「Brush」屬性,以便在MouseOver上更改它的值?

XAML:

<Button x:Class="WpfTest.EllipseButton" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:local="clr-namespace:WpfTest" 
     Style="{DynamicResource localStyle}" 
     Name="ellipseButton"> 

    <Button.Resources> 
    <Style x:Key="localStyle" 
      TargetType="local:EllipseButton"> 
     <Style.Setters> 
     <Setter Property="Template"> 
      <Setter.Value> 
      <ControlTemplate> 
       <Grid> 
       <Ellipse Fill="{Binding ElementName=ellipseButton, Path=Brush}" /> 
       </Grid> 
      </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     </Style.Setters> 
     <Style.Triggers> 
     <Trigger Property="IsMouseOver" 
       Value="True"> 

      <!-- ERROR HERE: The property "Brush" is not a dependency property. --> 
      <Setter Property="Brush" 
        Value="Blue" /> 
      <!-- ERROR HERE: The "BrushProperty" is not recognized or is not accessible. --> 
      <Setter Property="BrushPropety" 
        Value="Blue" /> 

     </Trigger> 
     </Style.Triggers> 
    </Style> 
    </Button.Resources> 
    <Grid> 
    </Grid> 
</Button> 

後臺代碼:

public partial class EllipseButton : Button 
{ 
    public static readonly DependencyProperty BrushProperty = DependencyProperty.Register(
     "Fill", 
     typeof(Brush), 
     typeof(EllipseButton), 
     new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Gray))); 

    public Brush Brush 
    { 
     get 
     { 
      return (Brush)GetValue(BrushProperty); 
     } 
     set 
     { 
      SetValue(BrushProperty, value); 
     } 
    } 

    public EllipseButton() 
    { 
     InitializeComponent(); 
    } 
} 

回答

1

你的財產被稱爲 「補」 不 「刷」:

public static readonly DependencyProperty BrushProperty = DependencyProperty.Register(
     "Fill", //Error is here 
     typeof(Brush), 
     typeof(EllipseButton), 
     new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Gray))); 

修改成:

public static readonly DependencyProperty BrushProperty = DependencyProperty.Register(
     "Brush", 
     typeof(Brush), 
     typeof(EllipseButton), 
     new FrameworkPropertyMetadata(new SolidColorBrush(Colors.Gray))); 
+0

感謝。我已經解決了這個問題,現在編譯器會抱怨:'EllipseButton'TargetType與元素'Button'的類型不匹配。但是,如果我將樣式的TargetType從本地:EllipseButton更改爲Button,則無法識別「Brush」屬性... – Libor 2013-04-20 21:59:33

+0

我已解決問題併發布了答案。所有新手的錯誤 - 閱讀Adam Nathan的「WPF 4 Unleashed」這本書並沒有讓我從所有這些麻煩中解脫出來:(...... Google和SO依然是唯一真正的幫手。 – Libor 2013-04-20 23:28:26

1

好了,需要幾件事情要做:

1)重命名依賴項屬性的名稱爲「刷」(它被錯誤地命名爲「填充」) - 由於HighCore

2)當使用控制在代碼中,移除設置「Brush」屬性 - 本地值從樣式中覆蓋setters。從風格的關鍵屬性,只保留類型名稱(仍不很:

3)移動從自定義控件樣式爲更高級別(例如,「主題\ Generic.xaml」下)

4)取出X知道爲什麼...)

5) 「刷」 屬性添加默認值的樣式setter方法(同樣,不知道爲什麼...)

固定EllipseButton.xaml:

<Button x:Class="WpfTest.EllipseButton" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> 
    <Grid/> 
</Button> 

f ixed後臺代碼:

public partial class EllipseButton 
{ 
    public static readonly DependencyProperty BrushProperty = DependencyProperty.Register(
     "Brush", 
     typeof(Brush), 
     typeof(EllipseButton), 
     new FrameworkPropertyMetadata(null)); 

    public Brush Brush 
    { 
     get 
     { 
      return (Brush)GetValue(BrushProperty); 
     } 
     set 
     { 
      SetValue(BrushProperty, value); 
     } 
    } 

    public EllipseButton() 
    { 
     InitializeComponent(); 
    } 
} 

定式(Generic.xaml):

<ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="clr-namespace:WpfTest"> 

    <Style TargetType="local:EllipseButton"> 
     <Style.Setters> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate> 
         <Grid> 
          <Ellipse Fill="{Binding ElementName=ellipseButton, Path=Brush}"/> 
         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="Brush" Value="Pink"/> 
     </Style.Setters> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Brush" Value="Red"/> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 

</ResourceDictionary>