询问者
数据模板中的TextBlock控件没有继承样式

问题
-
昨天遇到一个奇怪的问题,在ListBox的DataTemplate中使用的TextBlock没有继承统一的样式。
下面的示例中TextBox和Button都继承了统一的样式,但是TextBlock没有,不知道为什么。
代码如下:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.Resources> <Style TargetType="{x:Type TextBlock}"> <Setter Property="FontSize" Value="20"></Setter> <Setter Property="Foreground" Value="Blue"></Setter> <Setter Property="HorizontalAlignment" Value="Center"></Setter> <Setter Property="VerticalAlignment" Value="Center"></Setter> <Setter Property="Margin" Value="5"></Setter> </Style> <Style TargetType="{x:Type TextBox}"> <Setter Property="FontSize" Value="11"></Setter> <Setter Property="Foreground" Value="Red"></Setter> <Setter Property="MinHeight" Value="30"></Setter> <Setter Property="MinWidth" Value="100"></Setter> <Setter Property="HorizontalAlignment" Value="Center"></Setter> <Setter Property="VerticalAlignment" Value="Center"></Setter> <Setter Property="Margin" Value="5"></Setter> </Style> <Style TargetType="{x:Type Button}"> <Setter Property="FontSize" Value="15"></Setter> <Setter Property="Foreground" Value="Green"></Setter> <Setter Property="MinHeight" Value="30"></Setter> <Setter Property="MinWidth" Value="100"></Setter> <Setter Property="HorizontalAlignment" Value="Center"></Setter> <Setter Property="VerticalAlignment" Value="Center"></Setter> <Setter Property="Margin" Value="5"></Setter> </Style> <Style x:Key="listItemStyle"> </Style> </Window.Resources> <Grid> <Grid.RowDefinitions> <RowDefinition Height="100"></RowDefinition> <RowDefinition></RowDefinition> </Grid.RowDefinitions> <StackPanel Orientation="Horizontal"> <TextBlock Text="测试数据TextBlock"></TextBlock> <TextBox>测试</TextBox> <Button Content="ces"></Button> </StackPanel> <ListBox Grid.Row="1" Name="listbox"> <ListBox.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="测试数据TextBlock"></TextBlock> <TextBox Grid.Column="1" >测试</TextBox> <Button Grid.Column="2" Content="ces"></Button> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </Grid> </Window>
全部回复
-
Hi blwuer,据我所知,TextBlock直接继承于FrameworkElement, TextBlock属于比较底层的控件,因此它的性能要比其他继承Control类的控件好一些。但TextBlock只是纯文本的显示,并且不提供Access key的支持。
所以,我建议你将<ListBox.ItemTemplate>中模板的样式 统一放在 <DataTemplate.Resources> 中进行设置。
<ListBox Grid.Row="1" Name="listbox" ItemsSource="{Binding}" > <ListBox.ItemTemplate> <DataTemplate> <DataTemplate.Resources> <Style TargetType="TextBlock"> <Setter Property="FontSize" Value="20"></Setter> <Setter Property="Foreground" Value="Blue"></Setter> <Setter Property="HorizontalAlignment" Value="Center"></Setter> <Setter Property="VerticalAlignment" Value="Center"></Setter> <Setter Property="Margin" Value="5"></Setter> </Style> </DataTemplate.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> <ColumnDefinition></ColumnDefinition> </Grid.ColumnDefinitions> <TextBlock Grid.Column="0" Text="测试数据TextBlock"></TextBlock> <TextBox Grid.Column="1" Text="{Binding Path=name}" ></TextBox> <Button Grid.Column="2" Content="ces"></Button> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Best Regards,
Yohann Lu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com. -
Hi blwuer,据我了解,TextBlock,直接从FrameworkElement继承,因此错过了所有继承自Control的所有元素的共同行为。 TextBlock的浅层继承层次使得控件比Label更轻,更适合更简单,非交互的场景。
你可以参考微软官方文档去进一步了解TextBlock,并在适用的场合使用它。
TextBlock 类:
https://msdn.microsoft.com/zh-cn/library/system.windows.controls.textblock(v=vs.110).aspx
Best Regards,Yohann Lu
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.