2013-04-26 2 views
2

В моем списке Listview есть элементы, помеченные как метка. Я разрабатываю стиль для этого ярлыка, и я не знаю, как получить доступ к свойству IsSelected родительского (ListViewItem).Как получить доступ к свойствам родительского элемента управления в стиле

EDIT - попытался предложения ниже, но все еще получаю исключение, вот мой полный код:

<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

Вы должны быть в состоянии использовать RelativeSource:

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

EDIT: Попробуйте с помощью MultiDataTrigger вместо MultiTrigger а. Проверьте this.

+0

Я попробовал, но получил это исключение: «Связывание» не может быть установлено в свойстве «Свойство» типа «Условие». «Связывание» может быть установлено только на DependencyProperty объекта DependencyObject. –

+0

Я думаю, вы можете использовать 'RelativeSource = {RelativeSource FindAncestor}', чтобы найти точный ListView? – Fendy

+0

С этим я получаю следующее исключение: «Инициализация« System.Windows.Data.RelativeSource »сделала исключение». Номер линии «38» и позиция линии «37». –

0

Чтобы сэкономить время, отправляя синтаксис, который работал для меня, и для OP: {Binding RelativeSource = {RelativeSource AncestorType = {х: Тип ParentType}}, Path = ParentProperty}

Смежные вопросы