WPF - 从触发器访问儿童

问题描述:

我有一个ListBox声明像我在那里显示。关键在于,在项目定义中,我有大量的网格和元素。我想更改项目中某个图像的可见性,仅在选择元素本身时才可见。WPF - 从触发器访问儿童

我管理的选择时,它改变,例如,背景和一般看的项目,但我试图用我不能访问内部元件:(

<ListBox stuff stuff stuff> 
    <ListBox.ItemTemplate> 
     <DataTemplate DataType="local:Patient"> 
      <grids , borders, things, stuff 
       <Image Name="image1" source opacity stuff/> 
      </ grids bordes and design in general> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 

    <ListBox.ItemContainerStyle> 
     <Style TargetType="{x:Type ListBoxItem}"> 
      <Style.Triggers> 
       <Trigger Property="IsSelected" Value="True"> 
        <Setter Property="Foreground" Value="White"/> 
        <!--HERE I WANT TO CHANGE VISIBILITY OF THE IMAGE--> 
       </Trigger> 
      </Style.Triggers> 
     </Style> 
    </ListBox.ItemContainerStyle> 

    <ListBox.Template> 
     <!-- some other styles when deselected and other things --> 
    </ListBox.Template> 
</ListBox> 

<Setter TargetName="physiciansSettinsImage" Property="Visibility" Value="Visible"/> 

但是它不能在样式设置器上设置任何线索?

整个设计相当复杂,所以我想尽可能地避免重新编码它

将触发器移至DataTemplate

我想此搜索VisibilityCollapsed

<DataTemplate.Triggers> 
    <DataTrigger Binding="{Binding RelativeSource= 
     {RelativeSource Mode=FindAncestor, AncestorType= 
      {x:Type ListBoxItem}},Path=IsSelected}" Value="True"> 
     <Setter TargetName="image1" Property="Visibility" Value="Visible"/> 
    </DataTrigger> 
</DataTemplate.Triggers> 

现在,当您Image's Visibility设置为Visible被选择的项目。

+0

如此甜蜜的兄弟。快速和干净。我必须承认,整个事情对我来说有点困惑,但是......一点一点的:) – javirs 2013-03-26 15:32:20