Привязка TextBlock обратно к ListBoxItem ItemSource - PullRequest
0 голосов
/ 02 марта 2012

У меня есть стиль ListBoxItem, который я пытаюсь изменить, чтобы он отображал многоточие символов, когда поле списка становится маленьким.Для этого мне пришлось избавиться от ContentPresenter в нашем коде и заменить его на TextBlock.Все ListBox, к которым это применяется, связаны через свойство 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}")не работает.Какой должна быть привязка для правильной работы?

1 Ответ

0 голосов
/ 02 марта 2012

Ваш единственный разумный выбор - предположить, что контекст данных ListBoxItem является строкой или может отображаться следующим образом:

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