2016-10-25 4 views
1

ControlTemplateを作成したいと思います。 ContentPresenterのテキストはスタイリングされていなければなりません。 contentpresenterでテキストブロックのスタイルを設定するには?使用WPF:ContentPresenterにTextBlockスタイルを適用する

<Style x:Key="PrimaryPanel" TargetType="GroupBox"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="GroupBox"> 
        <Border BorderThickness="1" BorderBrush="#337AB7"> 
         <Grid> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto"/> 
           <RowDefinition Height="*"/> 
          </Grid.RowDefinitions> 
          <StackPanel Background="#337AB7"> 
           <ContentPresenter ContentSource="Header"> 
            <ContentPresenter.Resources> 
             <Style TargetType="{x:Type TextBlock}"> 
              <Setter Property="Foreground" Value="#fff"></Setter> 
              <Setter Property="Margin" Value="0 -1 0 0"></Setter> 
              <Setter Property="Padding" Value="5"></Setter> 
              <Setter Property="VerticalAlignment" Value="Center"></Setter> 
             </Style> 
            </ContentPresenter.Resources> 
           </ContentPresenter> 
          </StackPanel> 
          <Border Grid.Row="1" Padding="10 5" Margin="5 0 5 10" > 
           <StackPanel> 
            <ContentPresenter /> 
           </StackPanel> 
          </Border> 
         </Grid> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

:アドバイス

<GroupBox Style="{StaticResource PrimaryPanel}"> 
    <GroupBox.Header> 
     <TextBlock Text="Title"/> this text must by styled 
    </GroupBox.Header> 
    </GroupBox> 

おかげ これは私のコードです。

答えて

2

私はかなりよくあなたの問題を解決し、あなたのための簡単な動作を作成しました:

public class Behaviour 
{ 
    public static object GetStyleToLoad(DependencyObject obj) 
    { 
     return (object)obj.GetValue(StyleToLoadProperty); 
    } 

    public static void SetStyleToLoad(DependencyObject obj, object value) 
    { 
     obj.SetValue(StyleToLoadProperty, value); 
    } 

    // Using a DependencyProperty as the backing store for StyleToLoad. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty StyleToLoadProperty = 
     DependencyProperty.RegisterAttached("StyleToLoad", typeof(object), typeof(Behaviour), new PropertyMetadata(null, 
      (o, e) => 
      { 
       if (e.NewValue != null) 
       { 
        Style s = e.NewValue as Style; 
        if (s != null) 
        { 
         FrameworkElement fe = o as FrameworkElement; 

         if (fe != null) 
         { 
          fe.Resources.Add(s.TargetType, s); 
         } 
        } 
       } 
      })); 


} 

使用上:私の見解で

私はHotPinkフォアグラウンドでのTextBlockスタイルを置く:

<UserControl.Resources> 
    <Style TargetType="{x:Type TextBlock}"> 

     <Setter Property="Foreground" 
       Value="HotPink" /> 

    </Style> 

ContentPresenterの動作の適用:

あり
<ContentPresenter local:Behaviour.StyleToLoad="{StaticResource {x:Type TextBlock}}" 
         Content="My Text" /> 

コンパイル後の結果:

enter image description here

あなたはスタイルが適用されている見ることができるように。

関連する問題