Windows应用中的ContentPresenter中的TextTrimming

时间:2016-05-10 12:13:42

标签: xaml windows-store-apps

我在windows store app中编辑了ListBoxItem的样式。 当item的内容很长时我想修剪它,就像在textblock中我们有属性一样     TextTrimming =“CharacterEllipsis”。 我试过两种方式。

<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
    <TextBlock TextTrimming="WordEllipsis" Text="{TemplateBinding Content}"        Margin="{TemplateBinding Padding}"/>
</ContentPresenter>

<ContentPresenter x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
  <ContentPresenter.Resources>
    <Style TargetType="TextBlock">
      <Setter Property="TextTrimming" Value="CharacterEllipsis"/>
      <Setter Property="MaxWidth" Value="10"/>
      <Setter Property="Foreground" Value="Red"/>
    </Style>
  </ContentPresenter.Resources>
</ContentPresenter>

但不适合我,任何帮助都会感激。

2 个答案:

答案 0 :(得分:1)

首先,在XAML中设置ListBox属性ScrollViewer.HorizontalScrollBarVisibility="Disabled"

然后使用您的第一个XAML示例移除ContentPresenter包裹TextBlock(在此方案中您不需要它)并替换为<Grid>

答案 1 :(得分:1)

通过编辑ContentTemplate完成,

<ContentPresenter x:Name="ContentPresenter" ContentTransitions="{TemplateBinding ContentTransitions}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
      <ContentPresenter.ContentTemplate>
        <DataTemplate>
         <TextBlock Text="{Binding ElementName=ContentPresenter, Path=Content}" TextTrimming="CharacterEllipsis"/>
        </DataTemplate>
       </ContentPresenter.ContentTemplate>
</ContentPresenter>