如何访问控件样式中父项的属性

时间:2013-04-26 07:06:53

标签: c# wpf xaml listview multidatatrigger

我的Listview将项目datatemplated为标签。我正在为该标签设计一个样式,我不知道如何访问父类(ListViewItem)IsSelected属性。

编辑 - 尝试了下面的建议,但仍然有例外,这是我的完整代码:

 <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>

3 个答案:

答案 0 :(得分:3)

您应该可以使用RelativeSource

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

编辑: 尝试使用MultiDataTrigger代替MultiTrigger。检查this

答案 1 :(得分:0)

ListView已单独SelectedItemTemplate。所以你可以使用它。

答案 2 :(得分:0)

为了节省您的时间,发布适用于我和OP的语法: {Binding RelativeSource = {RelativeSource AncestorType = {x:Type ParentType}},Path = ParentProperty}