views:

464

answers:

1

I'm customising the appearance of grouping in a ListBox. In ListBox.Resources, I have declared something like (formatting removed):

<Style TargetType="{x:Type GroupItem}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type GroupItem}">
        <StackPanel Orientation="Vertical">
          <!-- Group label -->
          <ContentPresenter />
          <!-- Items in group -->
          <ItemsPresenter />
        </StackPanel>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

The actual group label is not very readable and I'd like to use a value converter to make it more presentable. However I cannot find a way to obtain this text and convert it.

I figure that a Binding would let me use a converter.

I've tried replacing the ContentPresenter above with the likes of...

<TextBlock Text="{TemplateBinding Content}"/>

<TextBlock Text="{Binding}"/>

...and numerous other things, but to no avail. Any suggestions?

A: 

Well isn't that just typical. I found the answer shortly after posting...

<TextBlock Text="{Binding Path=Content.Name,
  RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=GroupItem},
  Converter={StaticResource MyConverter}}"/>

Sometimes just the process of actually asking the question draws the answer out of thin air. In this case looking at the source code of GroupItem in .NET Reflector did the trick.

Hope someone else finds this edge case useful. Still, it would be a lot nicer if GroupItem exposed a property for this directly.

I'll still award a correct answer to anyone who knows a nicer way of doing this.

Drew Noakes