none
数据模板中的TextBlock控件没有继承样式 RRS feed

  • 问题

  • 昨天遇到一个奇怪的问题,在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>



    2016年12月27日 2:37

全部回复


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

    2016年12月27日 6:21
    版主
  • 我是想让模板中的样式不需要单独设置,这样可以保持整个程序的样式统一性。

    还有就是我想知道TextBox和Button的样式得到了继承,而TextBlock的样式没有继承,这中间的区别在哪儿,想了解的底层一些, 这样可以更好的理解模板、样式系统。

    2016年12月27日 6:50

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

    2016年12月28日 3:01
    版主