c# - CharacterEllipsis 在 ItemsControl WPF 中不起作用

标签 c# wpf textblock texttrimming

我试图在 ItemsControlDataTemplate 内的文本上设置 CharacterEllipsis

<Window x:Class="CustomPanel.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CustomPanel;assembly="
Title="Window1" Height="400" Width="400">
<Window.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="TextTrimming" Value="CharacterEllipsis"></Setter>
    </Style>

    <DataTemplate DataType="{x:Type local:Person}">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Name}"
                       Margin="100,0,0,0"/>
        </StackPanel>
    </DataTemplate>
</Window.Resources>

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <ItemsControl ItemsSource="{Binding Persons}" 
                  Grid.Column="0">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

</Grid>

我还尝试为 ItemsControlStackPanelTextBlock 本身设置宽度。

有什么想法吗?

编辑

为了强调这不是 StyleStackPanel 的问题,我删除了两者,但它仍然不起作用

<Window.Resources>
    <DataTemplate DataType="{x:Type local:Person}">
            <TextBlock Text="{Binding Name}"
                       TextTrimming="CharacterEllipsis"
                       Margin="100,0,0,0"
                       Width="200"/>
    </DataTemplate>
</Window.Resources>

<Grid>
    <ItemsControl ItemsSource="{Binding Persons}"/>
</Grid>

澄清

当文本太大时这非常有效,但我希望它在窗口调整到更小的宽度时也能正常工作。

最佳答案

DataTemplate 不会从窗口中选取样式,因为它不在该范围内。在 DataTemplate.ResourcesApplication.Resources 中移动样式,以便它可以被 DataTemplate 中的 TextBlock 选取.

其次,您已将 TextBlock 包裹在 StackPanel 中,这使其可以无限扩展。因此,移除包装 TextBlock 的 Stackpanel

第三,ItemsControl的约束宽度或者MaxWidth自己设置。

<Window.Resources>
    <DataTemplate DataType="{x:Type local:Person}">
        <DataTemplate.Resources>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
            </Style>
        </DataTemplate.Resources>
        <TextBlock Text="{Binding Name}"/>
    </DataTemplate>
</Window.Resources>
<ItemsControl ItemsSource="{Binding Persons}" Width="30">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel/>
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>

更新:

如果您想让它用于调整窗口大小,不要设置宽度,而是在 TextBlock 上设置最大宽度:

<Window.Resources>
    <DataTemplate DataType="{x:Type local:Person}">
        <TextBlock Text="{Binding Name}" MaxWidth="200"
                    TextTrimming="CharacterEllipsis"/>
    </DataTemplate>
</Window.Resources>
<ItemsControl Margin="100,0,0,0" ItemsSource="{Binding Persons}"/>

关于c# - CharacterEllipsis 在 ItemsControl WPF 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25378527/

相关文章:

c# - 元组 - LINQ to Entities 中仅支持无参数构造函数和初始值设定项

c# - 为什么我的命名 WPF 形状无法从代码隐藏中访问?

c# - WPF 中带有文本 block 的动态工具提示

c# - TextBlock 滚动在 WPF 中不起作用

c# - 为什么我的论点似乎被 vhost.exe 覆盖了

c# - 序列化两个互相引用的对象

wpf - 如何将 View 绑定(bind)到使用参数实例化的 View 模型?

c# - 将 MVVM 与(仅)从集合中自动生成的某些列绑定(bind)

wpf - ScaleTransform和CenterX

c# - For 循环填充文本 block