【问题标题】:WPF - Access children from triggerWPF - 从触发器访问子级
【发布时间】:2013-03-16 10:35:06
【问题描述】:

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

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

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

【问题讨论】:

    标签: c# wpf listbox styles itemcontainerstyle


    【解决方案1】:

    将触发器移至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 Visibility is set to Visible.

    【讨论】:

    • 好可爱的兄弟。又快又干净。我不得不承认整个事情对我来说有点令人困惑,但是......一点一点:)
    猜你喜欢
    • 1970-01-01
    • 2011-05-19
    • 1970-01-01
    • 2017-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    相关资源
    最近更新 更多