0

我在我的 Windows 资源区创建了一个样式:

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

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

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

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

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

这是完整的窗口 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>
4

1 回答 1

0

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

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.

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.

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

于 2012-04-19T15:18:40.283 回答