2013-03-26 88 views
4

我有一個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"/> 

但是它不能在樣式設置器上設置任何線索?

整個設計相當複雜,所以我想盡可能地避免重新編碼它

回答

5

將觸發器移至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