3

My Listview has items datatemplated as a label. I'm designing a style for that label and I don't know how to access the parent's(ListViewItem) IsSelected property.

EDIT - tried the suggestions below, but still getting an exception, here's my complete code:

 <Style x:Key="ListViewItemStyle" TargetType="ListViewItem">
                <Setter Property="SnapsToDevicePixels" Value="true"/>
                <Setter Property="OverridesDefaultStyle" Value="true"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                                <GridViewRowPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="{StaticResource WindowBorderBrush}"/>
                        <Setter Property="Foreground" Value="White"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
            <Style x:Key="GVLabelStyle"
                   BasedOn="{StaticResource LabelStyle}"
                   TargetType="Label">
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition  Property="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor}}" Value="True"/>
                        </MultiDataTrigger.Conditions>            
                        <Setter Property="Foreground" Value="White"/>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>

            <DataTemplate x:Key="appTemplate">
                <Label Style="{StaticResource GVLabelStyle}"
                       Content="{Binding ProcessInfo.ProcessName}">
                               </Label>
            </DataTemplate>  

 <ListView Background="Transparent"
                  Name="mainContentHolder"
                  ItemsSource="{Binding}"
                  BorderBrush="Transparent"
                  ItemContainerStyle="{StaticResource ListViewItemStyle}">
                <ListView.View>
                <GridView ColumnHeaderContainerStyle="{StaticResource HeaderStyle}">
                    <GridViewColumn Header="Application" 
                                    CellTemplate="{StaticResource appTemplate}"/>
                    <GridViewColumn Header="Window Title"
                                    CellTemplate="{StaticResource wndTemplate}"
                                    Width="300"/>
                    <GridViewColumn Header="Date"
                                    CellTemplate="{StaticResource dateTemplate}"/>

                </GridView>
            </ListView.View>

        </ListView>
Anatoliy Nikolaev
  • 22,370
  • 15
  • 69
  • 68
Marko Devcic
  • 1,069
  • 2
  • 13
  • 16

3 Answers3

4

You should be able to use RelativeSource:

<Condition Property="{Binding Path=IsSelected, RelativeSource={RelativeSource TemplatedParent}}" Value="True" />

EDIT: Try a using a MultiDataTrigger instead of a MultiTrigger as well. Check this.

Community
  • 1
  • 1
  • I tried that, but got this exception: A 'Binding' cannot be set on the 'Property' property of type 'Condition'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject. – Marko Devcic Apr 26 '13 at 07:28
  • I think you can use `RelativeSource={RelativeSource FindAncestor}` to find exact ListView? – Fendy Apr 26 '13 at 07:36
  • With that i get the following exception: 'Initialization of 'System.Windows.Data.RelativeSource' threw an exception.' Line number '38' and line position '37'. – Marko Devcic Apr 26 '13 at 07:48
  • 1
    Ok , got it to work ... Thank you .. this works now... – Marko Devcic Apr 26 '13 at 08:08
0

ListView has separate SelectedItemTemplate. So you could use it.

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
Anatolii Gabuza
  • 6,184
  • 2
  • 36
  • 54
0

To save you time, posting the syntax that worked for me and for OP: {Binding RelativeSource={RelativeSource AncestorType={x:Type ParentType}}, Path=ParentProperty}

user94592
  • 27
  • 1
  • 3