为什么没有应用我的文本块样式? [英] Why doesn't my text block style get applied?

查看:90
本文介绍了为什么没有应用我的文本块样式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows资源区域创建了一种样式:

I have a style created in my windows resources area:

<Style TargetType="TextBlock">
    <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
</Style>

我认为这意味着所有文本块都将具有这种样式,因此当我创建列表视图列时:

I thought this meant that all text blocks would have this style, so when I create my list view column:

<GridViewColumn>
    <GridViewColumnHeader Content="Source"/>
    <GridViewColumn.CellTemplate>
         <DataTemplate>
             <TextBlock Text="{Binding Source, Mode=OneWay}" />
         </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

省略号样式不适用于该列中的文本块.

The ellipsis style is NOT applied to the text block in the column.

如果我使用x:Key命名样式,然后使用Style = {StaticResource xxx},则它可以工作-为什么未命名方法不起作用?

If I name the style with x:Key, then use Style={StaticResource xxx} then it works - why doesn't the unnamed approach work?

这是XAML的完整窗口:

Here's the complete window XAML:

<Window x:Class="ListViewStyle.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <Style TargetType="TextBlock">
        <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
    </Style>
</Window.Resources>
<ListView ItemsSource="{Binding Rows}">
    <ListView.View>
        <GridView>
            <GridViewColumn>
                <GridViewColumnHeader Content="Source"/>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Source, Mode=OneWay}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn>
                <GridViewColumnHeader Content="Primary"/>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Primary, Mode=OneWay}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
            <GridViewColumn>
                <GridViewColumnHeader Content="Secondary"/>
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding Secondary, Mode=OneWay}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>
</Window>

推荐答案

您可能正在定义另一种TextBlock样式,更接近TextBlock本身.

You may be defining another TextBlock style, closer to the TextBlock itself.

WPF搜索样式的方式从要样式的项目开始.如果设置Style属性,则使用该属性.否则,它将使用找到的第一种样式来检查父对象的资源,然后检查其祖父母的资源,依此类推.如果在您的应用程序中找不到样式,则使用默认样式.

The way WPF searches for a style starts with the item to be styled. If it sets the Style property, then it uses that. Otherwise, it checks the parent's resources, then its grandparent's resources, and so on, using the first style it finds. If it doesn't find a style in your application it uses the default style.

TextBlock的祖先之一可能是TextBlock样式.如果在该处找到样式,则不会在窗口级别使用该样式.

There is probably a TextBlock style in one of the TextBlock's ancestors. If it finds a style there, it won't use the one at the window level.

您可以使用 BasedOn 属性.

You can specify that one style is based on another, with the BasedOn property.

这篇关于为什么没有应用我的文本块样式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆