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 条评论
登录 后参与评论

相关文章

来自专栏Hongten

python开发_pprint()

874
来自专栏C#

DocX在C#中的基本操作方法

    用了一个星期把园子里2016年中有关.net的文章都看了,有些只是大致的看了一下,在看的同时也在记录一些通用的方法。发现有很多对NPOI的文档,主...

1948
来自专栏c#开发者

Clean up your BizTalk databases

Here are a few scripts / stored procedures that most of the Biztalk newbies woul...

3394
来自专栏聊聊技术

原 初学算法-基于最小堆的优先级队列C++

3069
来自专栏IT开发技术与工作效率

VBA判断工作表是否存在&不存在时创建(Excel)

1944
来自专栏c#开发者

Using ASP.NET and jQuery to Pass Multiple Values from a GridView to Another Page

Using ASP.NET and jQuery to Pass Multiple Values from a GridView to Another Page...

3324
来自专栏各种机器学习基础算法

laravel5.4上传EXCEL并解析

首先,通过composer引入Laravel Excel v2.1库 composer require maatwebsite/excel ~2.1.0 将Se...

32712
来自专栏用户画像

AJAX +SpringMVC 实现bootstrap模态框的分页查询

612
来自专栏跟着阿笨一起玩NET

Error This project references NuGet package(s) that are missing on this computer. Use NuGet Package

Severity Code Description Project File Line Suppression State Error This project...

802
来自专栏码农阿宇

C# MVC的一种高效分页的html方法

首先创建一个html的扩展方法,这个方法是万能的,可以直接拿到您的项目中使用: //主要就是输出分页的超级链接的标签 //自定义分页Helpe...

43411

扫码关注云+社区