4

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

例如,当项目被选中时,我设法更改了项目的背景和总体外观,但我无法访问内部元素:(

<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"/>

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

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

4

1 回答 1

7

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

Now when the item is selected your Image's Visibilityis set to Visible.

于 2013-03-26T15:18:34.413 回答