zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Aizu 0033
    Aizu 0118
    【思维】贪心+细节——cf1361B
    【思维】构造+凸包+向量叉积——LEETCODE 游乐园的迷宫
    【思维】三元环计数+鸽笼定理/贪心——LEETCODE 游乐园的游览计划 好题
    dp+线性筛——LEETCODE切分数组
    【经典】带障碍的铺砖块——LEETCODE 覆盖
    【思维】树形dp+构造——leetcode二叉树任务调度
    【思维】状压dp—— 2020 联想杯 M
    【思维】建图+排列组合+预处理+最短路—— 2020 联想杯 E
  • 原文地址:https://www.cnblogs.com/smiler/p/3317581.html
Copyright © 2011-2022 走看看