2012-03-01 83 views
0

我有一个ListBoxItem样式,我试图修改它以便在列表框变小时显示字符省略号。为此,我不得不摆脱我们的代码中的ContentPresenter并将其替换为TextBlock。这应用于的ListBox都是通过ItemSource属性绑定的。将TextBlock绑定回ListBoxItem ItemSource

这是我的代码。

<Style x:Key="ListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="Background" Value="White"/> 
    <Setter Property="Margin" Value="0,0,0,0"/> 
    <Setter Property="Padding" Value="0,0,0,0"/> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Grid> 
        <Border x:Name="Bd" SnapsToDevicePixels="true"> 
         <!-- Before this used to be ContentPresenter but I switched it to TextBlock to get it the TextTrimming property. I can't find the right way to bind the data though.--> 
         <TextBlock Text="{TemplateBinding DisplayMemberPath}" TextTrimming="CharacterEllipsis" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
              SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
              VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
        </Border> 
        <Rectangle x:Name="HoverRectangle" 
           Stroke="{StaticResource Gold}" 
           StrokeDashCap="Square" 
           StrokeThickness="0" 
           SnapsToDevicePixels="True" /> 
        <Rectangle x:Name="KeyboardFocusRectangle" 
           Height="Auto" 
           SnapsToDevicePixels="True" 
           Stroke="{StaticResource BrightBlue}" 
           StrokeDashCap="Square" 
           StrokeThickness="0" /> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <!-- Bunch of Triggers in here --> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 

我当前的TextBlock文本绑定(Text =“{TemplateBinding DisplayMemberPath}”)不起作用。为了正确工作,绑定应该是什么?

+0

你埋葬你的“问题”,其中包括你的代码 - 我建议你编辑。 – 2012-03-01 21:07:40

回答

0

你唯一合理的选择这里是假设一个ListBoxItem的数据上下文是一个字符串,或可显示为这样:

<TextBlock Text="{Binding}" .../> 
+0

这没有真正解决我的问题,但它确实为我提供了指向正确方向的信息。我应该一直在为它创建一个DataTemplate。我做到了,解决了问题。谢谢! – Rumel 2012-03-01 22:04:37