2013-04-13 5 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> 

Я не понимаю, как & почему делает textblock text в stackpanel получает красный цвет. Может ли кто-нибудь объяснить мне, как и почему это происходит простыми словами?

здесь выход:

enter image description here

ответ

0

Это из-за

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

Foreground затем наследуются вниз по дереву элементов в ваш StackPanel, TextBlock и Button, а потому, что Button имеет Foreground установлен явно, это не красный цвет.

Смежные вопросы