WPF - 从触发器访问子项

jav*_*irs 4 c# wpf styles listbox itemcontainerstyle

我有一个ListBox声明,就像我在那里展示.关键是在Item定义中我有很多关于网格和元素的东西.我想仅更改项目中一个图像的可见性,使其仅在选择元素本身时才可见.

例如,我设法更改了项目的背景和一般外观,但是我无法访问内部元素:(

<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>
Run Code Online (Sandbox Code Playgroud)

我试过用:

<Setter TargetName="physiciansSettinsImage" Property="Visibility" Value="Visible"/>
Run Code Online (Sandbox Code Playgroud)

但它不能在Style Setter上设置.任何线索?

整个设计相当复杂,所以我想尽量避免重新编码.

Kap*_*íko 5

将触发器移动到DataTemplate.

我想image1 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>
Run Code Online (Sandbox Code Playgroud)

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