首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF Trigger for IsSelected in a DataTemplate for ListBox items

WPF Trigger for IsSelected in a DataTemplate for ListBox items

作者头像
hbbliyong
发布2018-03-05 16:58:35
1.7K0
发布2018-03-05 16:58:35
举报
文章被收录于专栏:hbbliyonghbbliyong
<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>
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2013-09-12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档