2

I've a ItemsControl that I bind to my viewmodel, but inside the datatemplate I also have an image. I want that image to be visible as long as it's not the last item in the list, then it should be hidden (it's an arrow that point down to the next control).

The xaml look like this:

<ItemsControl ItemsSource="{Binding PageContainers}" x:Name="Items">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <controls:DesignControl DataContext="{Binding}" MouseDown="UIElement_OnMouseDown" MouseUp="UIElement_OnMouseUp" MouseMove="UIElement_OnMouseMove"/>
                <Image Source="/Resources/Images/arrow.png" Height="16" Width="16" Margin="0,10,0,0"/>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

So is there any easy to check if the Image/Stackpanel is last in the list? I guess I could subscribe to some event and do it in the code behind, but I guess it's cleaner if I could do it inside the xaml.

Yael
  • 1,566
  • 3
  • 18
  • 25
MilleB
  • 1,470
  • 2
  • 19
  • 32
  • The cleaner way to do it is with binding on the view your are already attached to. Having a visibility property attached to the items of the collection – Franck Jan 19 '15 at 20:28
  • 1
    I guess it could be a duplicate of this one http://stackoverflow.com/questions/7767097/use-different-template-for-last-item-in-a-wpf-itemscontrol – Icepickle Jan 19 '15 at 20:31

1 Answers1

2

You're binding to PageContainers which I assume to be a collection. Can the type of that collection be extended to include an IsLast property?

If it can, you can bind the visibility to that.

kidshaw
  • 3,423
  • 2
  • 16
  • 28
  • I tried out this solution and it works okay (by subscribing to CollectionChanged and then setting a IsLast property). But will keep the question open some more too see if there are any better solutions. – MilleB Jan 19 '15 at 20:44