WPF -从触发器访问子节点



我声明了一个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>

我试着使用:

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

但是它不能在样式设置器上设置。有线索吗?

整个设计相当复杂,所以我希望尽可能避免重新编码。

将触发器移至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>

现在当项目被选中时,你的Image's Visibility被设置为Visible

最新更新