2013-04-13 80 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明确地设置它不是红色的。