2013-04-13 81 views
0

我正在通過繼承ContentControl來創建自定義控件。下面是目前的XAML代碼Generic.xaml自定義控件綁定查詢

<Style TargetType="local:MyCustomControl"> 
    <Setter Property="Background" Value="YellowGreen"></Setter> 
    <Setter Property="IconSource" Value="/Themes/icon.png"></Setter> 

    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="local:MyCustomControl"> 
       <Border BorderBrush="White" BorderThickness="2"> 
        <Grid> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition Width="100"/> 
          <ColumnDefinition Width="*"/> 
         </Grid.ColumnDefinitions> 
         <Image Grid.Column="0" Source="{TemplateBinding IconSource}" Stretch="None"></Image> 

         <Border Background="{TemplateBinding Background}" Grid.Column="1"> 
          <ContentPresenter x:Name="ContentContainer" 
               Content="{TemplateBinding Content}" 
               ContentTemplate="{TemplateBinding ContentTemplate}" 
               Margin="{TemplateBinding Padding}"> 
          </ContentPresenter> 
         </Border> 
        </Grid> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 

    </Setter> 
</Style> 

在我的應用程序,我已經使用了自定義的控制是這樣的:

<mycontrol:MyCustomControl Height="100" Width="400" 
           Foreground="Red" 
           IconSource="/Assets/ApplicationIcon.png"> 
     <mycontrol:MyCustomControl.Content> 
      <StackPanel> 
       <TextBlock Text="Some Text Content Here"/> 
       <Button Content="Test Button" Foreground="Pink" Background="Black"/> 
      </StackPanel> 
     </mycontrol:MyCustomControl.Content> 
    </mycontrol:MyCustomControl> 

我不明白怎麼&爲什麼做的stackpanel中的文本塊文本變爲紅色。有人能夠用簡單的話來解釋我是如何以及爲什麼發生這種事的嗎

這裏是輸出:

enter image description here

回答

0

這是因爲

<mycontrol:MyCustomControl Height="100" Width="400" 
          Foreground="Red" 
          IconSource="/Assets/ApplicationIcon.png"> 

Foreground然後向下繼承的元素樹到您StackPanelTextBlockButton,但由於ButtonForeground明確地設置它不是紅色的。