0

I have a LongListSelector with a ItemTemplate like this:

<LongListSelector.ItemTemplate>
    <DataTemplate>
       <StackPanel>

          Here I have a lot of elements that are always the same.
          .
          .
          .
          and one that varies

       </StackPanel>
    </DataTemplate>     
</LongListSelector.ItemTemplate>

I know how to use this TemplateSelector class to change all the content inside the ItemTemplate depending o the item type.

My question is, how can use the template selector to change only the one item that varies so I dont need to repeat the whole lot of other elements.

I have tried using a ContentControl and ContentPresenter inside de StackPanel with no success

Marcelo de Aguiar
  • 1,432
  • 12
  • 31

1 Answers1

-1

You're going to want to create a data trigger in your XAML to check whether each item is the one you're looking for.

This SO question is relevant enough to get you on the right track. It'll end up looking something like this:

<Style TargetType="TabItem">
        <Style.Triggers>
            <!-- Styling for elements that are NOT the last item -->
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource LastItemConverter}}" Value="False">
                <Setter Property="Template">
                    <Setter.Value>
                        .
                        .
                        .
                    </Setter.Value>
                </Setter>
             </DataTrigger>
            <!-- Styling for elements that are the last item -->
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Converter={StaticResource LastItemConverter}}" Value="True">
                <Setter Property="Template">
                    <Setter.Value>
                        .
                        .
                        .
                    </Setter.Value>
                </Setter>
             </DataTrigger>

The Converter looks something like this - depending on what UI element you're binding.

public class IsLastItemInContainerConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
                          object parameter, CultureInfo culture)
    {
        DependencyObject item = (DependencyObject)value;
        TabControl tc = (TabControl)TabControl.ItemsControlFromItemContainer(item);


        // Return true for the first and last element 
        // (so they are similarly styled compared to other elements).
        if (tc.ItemContainerGenerator.IndexFromContainer(item) == tc.Items.Count - 1) {
            return true;
        }
        else if (tc.ItemContainerGenerator.IndexFromContainer(item) == 0)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

Hope that helps.

Community
  • 1
  • 1
Matt
  • 437
  • 3
  • 10
  • I dont want to style one specific item of the list. What I want is to reuse most of the DataTemplate except for one element. It like a DataTemplate inside a DataTemplate. – Marcelo de Aguiar Aug 08 '14 at 18:47