WPF:为ContentPresenter应用TextBlock样式

时间:2016-10-25 05:47:20

标签: wpf styles controltemplate

我想创建一个ControlTemplate。必须对ContentPresenter中的文本进行样式设置,但不要。如何在contentpresenter中设置文本块样式? 这是我的代码:

[1] Did not detect a `bs-config.json` or `bs-config.js` override file. Using lite-server defaults...
[1] ** browser-sync config **
[1] { injectChanges: false,
[1]   files: [ './**/*.{html,htm,css,js}' ],
[1]   watchOptions: { ignored: 'node_modules' },
[1]   server: { baseDir: './', middleware: [ [Function], [Function] ] } }
[1] [BS] Access URLs:
[1]  --------------------------------------
[1]        Local: http://localhost:3000
[1]     External: http://10.243.111.72:3000
[1]  --------------------------------------
[1]           UI: http://localhost:3001
[1]  UI External: http://10.243.111.72:3001
[1]  --------------------------------------
[1] [BS] Serving files from: ./
[1] [BS] Watching files...
[1] 16.10.25 13:16:35 200 GET /index.html
[1] 16.10.25 13:16:35 304 GET /node_modules/core-js/client/shim.min.js
[1] 16.10.25 13:16:35 304 GET /node_modules/reflect-metadata/Reflect.js
[1] 16.10.25 13:16:35 304 GET /node_modules/zone.js/dist/zone.js
[1] 16.10.25 13:16:35 304 GET /node_modules/systemjs/dist/system.src.js
[1] 16.10.25 13:16:35 200 GET /styles.css

使用:

    <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>

感谢您的建议。

1 个答案:

答案 0 :(得分: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 Foreground的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

正如您所见,风格已经应用。