WPF:为 ContentPresenter 应用 TextBlock 样式
WPF: Apply TextBlock Style for ContentPresenter
我想创建一个控件模板。 ContentPresenter 中的文本必须设置样式,但不要设置。如何在 contentpresenter 中设置文本块的样式?
这是我的代码:
<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>
谢谢指教。
我已经为您创建了一个简单的行为,可以很好地解决您的问题:
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" />
编译后结果:
如您所见,已应用样式。
我想创建一个控件模板。 ContentPresenter 中的文本必须设置样式,但不要设置。如何在 contentpresenter 中设置文本块的样式? 这是我的代码:
<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>
谢谢指教。
我已经为您创建了一个简单的行为,可以很好地解决您的问题:
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" />
编译后结果:
如您所见,已应用样式。