Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to the IsSelected property of the parent ListViewItem

I'm attempting to bind the Visibility property of a TextBlock that's held within the ItemTemplate for a ListView to the IsSelected property of the TextBlock's parent ListViewItem.

With this markup, the TextBlock is always visible.

<ListView x:Name="ItemListView" ItemsSource="{Binding Path=Accounts}" Margin="60,0,0,10" Grid.Row="1" Grid.Column="0">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="100">
                    </ColumnDefinition>
                    <ColumnDefinition Width="*"></ColumnDefinition>
                    <ColumnDefinition Width="200"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Image Width="100" Height="100" Grid.Column="0"></Image>
                <StackPanel Grid.Column="1">
                    <TextBlock Text="{Binding Path=Account.Name}"  
                                FontSize="24" Margin="5,0,0,0" TextWrapping="Wrap" />
                </StackPanel>
                <TextBlock Grid.Column="3" VerticalAlignment="Bottom"
                            Visibility="{Binding RelativeSource={RelativeSource Mode=TemplatedParent}, Path=IsSelected, Converter={StaticResource boolConverter}, Mode=OneWay}">
                    Show More Details...
                </TextBlock>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Notes: 1. In case it makes any difference, this is WinRT; a Metro app written in C#. 2. boolConverter is a fairly standard converter appears to work correctly.

like image 577
Rob Avatar asked Oct 24 '11 09:10

Rob


2 Answers

Use Mode=FindAncestor:

<TextBlock Grid.Column="3" VerticalAlignment="Bottom"
          Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem}, Path=IsSelected, Converter={StaticResource boolConverter}, Mode=OneWay}">
like image 158
Thomas Levesque Avatar answered Sep 21 '22 19:09

Thomas Levesque


I think that in this case you will have to use ElementName=ItemListView

like image 37
Murven Avatar answered Sep 19 '22 19:09

Murven