Wpf DataTemplate中的UserControl不';t字体的应用样式
我在Wpf DataTemplate中的UserControl不';t字体的应用样式,wpf,user-controls,resources,datatemplate,styles,Wpf,User Controls,Resources,Datatemplate,Styles,我在数据模板中有一个用户控件,文本块的样式不会更改字体大小,但会更改背景 随附样品: 创建一个WPF窗口 创建用户控件,UserControl1 在窗口内粘贴以下代码: <Window.Resources> <Style TargetType="{x:Type TextBlock}" x:Key="TextBlockStyleFontAndBackgound"> <Setter Property="FontSize"
数据模板
中有一个用户控件,文本块
的样式
不会更改字体大小
,但会更改背景
随附样品:
UserControl1
<Window.Resources>
<Style TargetType="{x:Type TextBlock}"
x:Key="TextBlockStyleFontAndBackgound">
<Setter Property="FontSize"
Value="20" />
<Setter Property="Background"
Value="Blue" />
</Style>
<DataTemplate x:Key="contentTemplate">
<StackPanel>
<m:UserControl1 />
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<ContentControl FontSize="10">
<StackPanel x:Name="stackPanel">
<Button Click="Button_Click" />
<ContentControl ContentTemplate="{StaticResource contentTemplate}" />
<!--<m:UserControl1 />-->
</StackPanel>
</ContentControl>
</Grid>
现在,Font
设置为样式TextBlockStyleFontAndBackgound
,大小为20
为什么现在FontSize
取自样式TextBlockStyleFontAndBackgound
谢谢,
巴拉克你在那里发现了一个非常奇怪的问题。我不确定为什么
FontSize
不在数据模板中时不受影响。。。查看MSDN上的两个属性描述和注释,它们之间的唯一区别是TextBlock.FontSize
也是一个AttachedProperty
,但我看不出这会对任何内容产生什么影响
不过,如果您仍然感兴趣,我可以为您提供一个解决方案。尝试在App.xaml
文件中声明您的样式
:
<Application.Resources>
<Style TargetType="{x:Type TextBlock}" x:Key="TextBlockStyleFontAndBackgound">
<Setter Property="FontSize" Value="20" />
<Setter Property="Background" Value="Blue" />
</Style>
</Application.Resources>
private void Button_Click(object sender, RoutedEventArgs e)
{
Style style = FindResource("TextBlockStyleFontAndBackgound") as Style;
textBlockInControl.Style = null;
textBlockInControl.Style = style;
}
<Application.Resources>
<Style TargetType="{x:Type TextBlock}" x:Key="TextBlockStyleFontAndBackgound">
<Setter Property="FontSize" Value="20" />
<Setter Property="Background" Value="Blue" />
</Style>
</Application.Resources>
<TextBlock Text="text" Style="{StaticResource TextBlockStyleFontAndBackgound}" />