Search code examples
wpfxamldatatemplate

Hiding A Specific-Indexed Element In An ItemsControl's ItemTemplate


In my WPF/MVVM app, I've got a ListBox with an ItemTemplate; I'm trying to figure out a way to hide a particular element in the template, only for the 0th item in the list. First, some simplified boilerplate:

<ListBox ItemsSource="{Binding MyItems}" AlternationCount="999999" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Text="{Binding Path=(ItemsControl.AlternationIndex), RelativeSource={RelativeSource AncestorType=ListBoxItem}, StringFormat='{}{0}: '}" />
                <TextBlock Grid.Column="1" Text="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Now let's say I want to hide the "Name" for just the 0th item. As I'd ideally hoped to do this entirely in XAML, I was attempting to leverage an extremely handy extension I often use, CalcBinding, which lets you write calculated binding expressions in XAML (note: it automatically converts between boolean & visibility for you):

<Button Content="Cancel" Command="{Binding CancelCommand}" Visibility="{c:Binding 'ProgressPercent > 0'}"/>

So in this case, I'd thought of something like:

<a:EnumEditBox Visibility="{c:Binding Path='(ItemsControl.AlternationIndex) == 0', RelativeSource={RelativeSource AncestorType=ListBoxItem}}" />

However, this and all other attempts seem to yield errors. In this case, the error is: BindingExpression path error: 'ItemsControl' property not found on 'object' ''ListBoxItem' (Name='')'. BindingExpression:Path=ItemsControl.AlternationIndex; DataItem='ListBoxItem' (Name=''); target element is 'EnumEditBox' (Name=''); target property is 'Visibility' (type 'Visibility')

So my questions are:

  1. Is it possible to do what I'm attempting, solely in XAML?
  2. If not, how might I otherwise accomplish this (to hide an element for the 0-indexed item in a ListBox)?

Any pointers would be greatly appreciated :)


Solution

  • The element in the ItemTemplate could have a Style with a DataTrigger on the AlternationIndex property:

    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                ...
                <TextBlock Text="{Binding Name}">
                    <TextBlock.Style>
                        <Style TargetType="TextBlock">
                            <Style.Triggers>
                                <DataTrigger
                                    Binding="{Binding Path=(ItemsControl.AlternationIndex),
                                              RelativeSource={RelativeSource AncestorType=ListBoxItem}}"
                                    Value="0">
                                    <Setter Property="Visibility" Value="Collapsed"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </TextBlock.Style>
                </TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>