【发布时间】:2014-10-12 06:21:36
【问题描述】:
我正在尝试在 ItemsControl 的 DataTemplate 内的文本上设置 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>
我还尝试将宽度设置为 ItemsControl、StackPanel 和 TextBlock 本身。
有什么想法吗?
编辑
为了强调这不是 Style 或 StackPanel 的问题,我将两者都删除了,但它仍然不起作用
<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>
澄清
这在文本太大时工作得很好,但我希望它在窗口也被调整为更小的宽度时工作。
【问题讨论】:
-
哇!您现在才提到问题的关键部分???在 4 个人花了半个小时试图帮助你之后?
-
@Sheridan 对不起。有时我在解释自己时有点问题:/
-
实际上,我刚刚测试过,但这也没有什么区别...我在不让它工作时遇到了麻烦...甚至 与
StackPanel作为ItemsPanelTemplate。 -
@Omribitan Person 类是如何定义的?列表是如何定义的,在哪里定义的?
标签: c# wpf textblock texttrimming