WPF Trigger for IsSelected in a DataTemplate for ListBox items

<DataTemplate DataType="{x:Type vm:HeaderSlugViewModel}">
        <vw:HeaderSlugView />
    </DataTemplate>

    <DataTemplate DataType="{x:Type vm:ContentSlugViewModel}">
        <vw:ContentSlugView />
    </DataTemplate>

    <DataTemplate DataType="{x:Type vm:ImageSlugViewModel}">
        <vw:ImageSlugView />
    </DataTemplate>
Each "View" is an independent XAML file. I'd like to be able to set up the triggers in those files, looking at the ListBoxItem's IsSelected property, in order to control the visibility of the various controls within.

The template to override the ListBoxItem follows:

    <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListBoxItem}">
                    <Border Name="SlugContainer" Background="Transparent" BorderBrush="Black" BorderThickness="1" CornerRadius="2" Margin="0,5,0,0" Padding="5">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*" />
                            </Grid.ColumnDefinitions>

                            <!-- snipped for brevity -->

                            <ContentPresenter Grid.Row="1" />

                        </Grid>
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
I modified the ContentPresenter in the following way in order to test out using the "FindAncestor" RelativeSource:

<ContentPresenter Grid.Row="1">
    <ContentPresenter.Style>
        <Style TargetType="{x:Type ContentPresenter}">
            <Setter Property="Visibility" Value="Collapsed" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="False">
                    <Setter Property="Visibility" Value="Collapsed"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentPresenter.Style>
</ContentPresenter>
This works, but when I move similar code into the XAML file representing a View it no longer sees the trigger. For example:

<UserControl ...>
    <UserControl.Resources>
        <local:FlowDocumentToXamlConverter x:Key="flowDocumentConverter" />
    </UserControl.Resources>

    <UserControl.Style>
        <Style TargetType="{x:Type UserControl}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}">
                    <Setter Property="Visibility" Value="Hidden" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </UserControl.Style>

    <DockPanel Name="SlugContainer">
        <Label DockPanel.Dock="Top" Content="Filler" />
        <ctrl:BindableRichTextBox x:Name="TextBox" Document="{Binding Content, Converter={StaticResource flowDocumentConverter}, Mode=TwoWay}" LostFocus="OnLostFocus" />
    </DockPanel>
</UserControl>
How can I detect, preferably from the View's XAML file, when the ListBoxItem has been selected?
I notice that DataTrigger.Value property is missing in UserContrl's Style. With the following changes, I think your code would work.

    <Style TargetType="{x:Type UserControl}">
      <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}, Path=IsSelected}" Value="True">
          <Setter Property="Visibility" Value="Hidden" />
        </DataTrigger>
      </Style.Triggers>
    </Style>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏SAP最佳业务实践

SAP S/4 HANA新变化-COPA

Profitability Analysis The universal journal (ACDOCA) is the heart of Accounting...

35113
来自专栏智能计算时代

deepstream 2.0 outperforms socket.io by more than x 1000

By Wolfram Hempel November 21st 2016 Realtime is growing fast. From collaborativ...

2034
来自专栏杨龙飞前端

underscore.js,js工具库

2655
来自专栏运维技术迷

shell脚本监控磁盘使用率

简单的一个监控磁盘分区使用率的脚本,欢迎批评指正. #!/bin/sh # df -h:查看硬盘信息 # sed '1d':删除第一行 # awk '{prin...

3367
来自专栏c#开发者

How to crop scanned barcode using Zbar?

http://pastebin.com/wSVW1tRc CGRect scanCrop The region of the video image tha...

3017
来自专栏Albert陈凯

Facilitator as Coach, Teacher, Trainer, and Mentor

As facilitators, I think most of us will agree that our foremost role is this: t...

3469
来自专栏xingoo, 一个梦想做发明家的程序员

【插件开发】—— 6 SWT 复杂控件使用以及布局

前文回顾: 1 插件学习篇 2 简单的建立插件工程以及模型文件分析 3 利用扩展点,开发透视图 4 SWT编程须知 5 SWT简单控件的使用与布局搭...

2079
来自专栏WindCoder

How to Build an Effective Initial Deployment Pipeline

I love building things—what developer doesn’t? I love thinking up solutions to i...

221
来自专栏marsggbo

Udacity并行计算课程 CS344 编程作业答案

592
来自专栏专注数据中心高性能网络技术研发

[Repost]The care and maintenance of your adviser

Published online  26 January 2011This article was originally published in the jo...

26511

扫描关注云+社区