MultiTrigger Условие для одинакового связывания / переключения управления с помощью триггера - PullRequest
1 голос
/ 08 ноября 2019

Я пытаюсь p:MarqueeTextBlock прокручивать только на ListBox IsSelected в TargetType="{x:Type ListBoxItem}".

Не удается выработать добавление условий MultiTrigger (срабатывает, когда IsSelected становитсяTrue или False), чтобы изменить Property="Scroll" из p:MarqueeTextBlock на Off или LeftTypewriter. Кроме того, не уверен, как ссылаться на p:MarqueeTextBlock в DataTemplate;возможно, если я предоставлю это x:Name? Однако я получаю Template property has already been set on DataTemplate.

Мне нужно как-то добавить AOTbMain_MARQUEE в x:Key="ListBoxStyle_GAME", например:

<p:MarqueeTextBlock Text="{Binding Path=Title}" Style="{StaticResource AOTbMain_MARQUEE}"/>

Код с указанной ссылкой не добавлен:

<UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:p="clr-namespace:Plugin.Controls;assembly=Plugin.v1"
    mc:Ignorable="d"
    d:DesignHeight="2160" d:DesignWidth="3840">

    <UserControl.Resources>
        <ResourceDictionary>
            <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Grid>
                                <Image x:Name="SelectedGameOverlay" Source="{}pack://siteoforigin:,,,/Images/Selected Game Overlay.png" RenderOptions.BitmapScalingMode="HighQuality" Visibility="Hidden" />
                                <ContentPresenter x:Name="SelectedGameTextColour" HorizontalAlignment="Left" />
                            </Grid>
                            <ControlTemplate.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Selector.IsSelectionActive" Value="False" />
                                        <Condition Property="IsSelected" Value="True" />
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Opacity" Value="1" />
                                    <Setter TargetName="SelectedGameOverlay" Property="Visibility" Value="Visible" />
                                    <Setter TargetName="SelectedGameTextColour" Property="TextElement.Foreground" Value="#FFFFFF" />
                                </MultiTrigger>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="Selector.IsSelectionActive" Value="True" />
                                        <Condition Property="IsSelected" Value="True" />
                                    </MultiTrigger.Conditions>
                                    <Setter Property="Opacity" Value="1" />
                                    <Setter TargetName="SelectedGameOverlay" Property="Visibility" Value="Visible" />
                                    <Setter TargetName="SelectedGameTextColour" Property="TextElement.Foreground" Value="#FFFFFF" />
                                </MultiTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style x:Key="ListBoxStyle" TargetType="p:AutoscrollListBox">
                <Setter Property="ItemContainerStyle" Value="{StaticResource ListBoxItemStyle}" />
                <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
                <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled" />
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="BorderThickness" Value="0" />
            </Style>

            <Style x:Key="ListBoxStyle_BASE" TargetType="p:AutoscrollListBox" BasedOn="{StaticResource ListBoxStyle}">
                <Setter Property="ScrollMode" Value="Center" />
                <Setter Property="ItemContainerStyle">
                    <Setter.Value>
                        <Style TargetType="ListBoxItem" BasedOn="{StaticResource ListBoxItemStyle}">
                            <Setter Property="Padding" Value="1" />
                        </Style>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style x:Key="AOTbMain_MARQUEE" TargetType="p:MarqueeTextBlock">
                <Setter Property="Scroll" Value="LeftTypewriter" />
                <Setter Property="ScrollDelayBeg" Value="0:0:5" />
                <Setter Property="ScrollDelayEnd" Value="0:0:3" />
                <Setter Property="ScrollSpeed" Value="0:0:5" />
            </Style>

            <Style x:Key="ListBoxStyle_BASE_INDXFADER" TargetType="p:AutoscrollListBox" BasedOn="{StaticResource ListBoxStyle_BASE}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IndexVisibility}" Value="Visible">
                        <Setter Property="Opacity" Value="0.1" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding IndexVisibility}" Value="Hidden">
                        <Setter Property="Opacity" Value="1" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>

            <Style x:Key="ListBoxStyle_BASETB" TargetType="p:AutoscaleTextBlock">
                <Setter Property="TextTrimming" Value="CharacterEllipsis" />
                <Setter Property="TargetFontSize" Value="30" />
            </Style>

            <Style x:Key="ListBoxStyle_GAME" TargetType="p:AutoscrollListBox" BasedOn="{StaticResource ListBoxStyle_BASE_INDXFADER}">
                <Setter Property="ItemTemplate">
                    <Setter.Value>
                        <DataTemplate>
                            <p:AutoscaleTextBlock Text="{Binding Path=Title}" Style="{StaticResource ListBoxStyle_BASETB}" />
                        </DataTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style x:Key="ListBoxItemStyleSearch" TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type ListBoxItem}">
                            <Grid>
                                <ContentPresenter x:Name="SelectedSearchTextColour" />
                            </Grid>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True" />
                                    <Setter Property="Opacity" Value="1" />
                                    <Setter TargetName="SelectedSearchTextColour" Property="TextElement.Foreground" Value="#FFFFFF" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>

            <Style x:Key="ListBoxStyleSearch" TargetType="p:AutoscrollListBox">
                <Setter Property="ItemContainerStyle" Value="{StaticResource ListBoxItemStyleSearch}" />
                <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
                <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled" />
                <Setter Property="BorderBrush" Value="Transparent" />
                <Setter Property="Background" Value="Transparent" />
            </Style>

            <Style x:Key="ListBoxStyle_INDX" TargetType="p:AutoscrollListBox" BasedOn="{StaticResource ListBoxStyleSearch}">
                <Setter Property="ItemContainerStyle">
                    <Setter.Value>
                        <Style TargetType="ListBoxItem" BasedOn="{StaticResource ListBoxItemStyleSearch}">
                            <Setter Property="Padding" Value="1" />
                        </Style>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <Viewbox HorizontalAlignment="Left">
            <p:AutoscrollListBox Name="Index" Style="{StaticResource ListBoxStyle_INDX}" Visibility="{Binding IndexVisibility}" />
        </Viewbox>

        <p:AutoscrollListBox Name="Items" Style="{StaticResource ListBoxStyle_GAME}" />
    </Grid>
</UserControl>

1 Ответ

1 голос
/ 08 ноября 2019

Следующий Style покажет AutoscrollListBox как содержимое DataTemplate. После выбора ListBoxItem вместо него будет отображаться MarqueeTextBlock. Если он не выбран, он переключится обратно на AutoscrollListBox.

Решение 1

<Grid>
  <AutoscrollListBox Name="Items" >
    <AutoscrollListBox.ItemTemplate>
      <DataTemplate>
        <DataTemplate.Resources>
          <AutoscaleTextBlock x:Key="AutoscaleTextBlock" 
                              Text="{Binding Title}" 
                              Style="{StaticResource ListBoxStyle_BASETB}"
                              x:Shared="False" />
          <MarqueeTextBlock x:Key="MarqueeTextBlock"
                            Text="{Binding Title}"                           
                            Style="{StaticResource AOTbMain_MARQUEE}"
                            x:Shared="False" />
        </DataTemplate.Resources>

        <ContentControl x:Name="ContentPresenter" />

        <DataTemplate.Triggers>
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}" 
                       Value="False">
            <Setter TargetName="ContentPresenter" 
                    Property="Content" 
                    Value="{StaticResource AutoscaleTextBlock}"/>
          </DataTrigger>
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}" 
                       Value="True">
            <Setter TargetName="ContentPresenter" 
                    Property="Content" 
                    Value="{StaticResource MarqueeTextBlock}"/>
          </DataTrigger>
        </DataTemplate.Triggers>
      </DataTemplate>
    </AutoscrollListBox.ItemTemplate>
  </AutoscrollListBox>
</Grid>

Решение 2
В случае, если выработать со словарями вложенных ресурсов и не может применить атрибут x:Shared, вы можете поменять элементы управления непосредственно из Trigger:

<Grid>
  <AutoscrollListBox Name="Items" >
    <AutoscrollListBox.ItemTemplate>
      <DataTemplate>
        <ContentControl x:Name="ContentPresenter" />

        <DataTemplate.Triggers>
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}" 
                       Value="False">
            <Setter TargetName="ContentPresenter" 
                    Property="Content">
              <Setter.Value>
                <AutoscaleTextBlock Text="{Binding Title}"                           
                                    Style="{StaticResource AOTbMain_MARQUEE}" />
               </Setter.Value>
             </Setter>
          </DataTrigger>
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}" 
                       Value="True">
            <Setter TargetName="ContentPresenter" 
                    Property="Content">
              <Setter.Value>
                <MarqueeTextBlock Text="{Binding Title}"                           
                                  Style="{StaticResource AOTbMain_MARQUEE}"/>
               </Setter.Value>
             </Setter>
          </DataTrigger>
        </DataTemplate.Triggers>
      </DataTemplate>
    </AutoscrollListBox.ItemTemplate>
  </AutoscrollListBox>
</Grid>

Solution 3
или переключитьVisibility:

<Grid>
  <AutoscrollListBox Name="Items" >
    <AutoscrollListBox.ItemTemplate>
      <DataTemplate>
        <Grid>
          <AutoscaleTextBlock x:Name="AutoscaleTextBlock" 
                              Text="{Binding Title}"                           
                              Style="{StaticResource AOTbMain_MARQUEE}"
                              Visibilty="Visible" />
           <MarqueeTextBlock x:Name="MarqueeTextBlock" 
                             Text="{Binding Title}"                           
                             Style="{StaticResource AOTbMain_MARQUEE}"
                             Visibilty="Collapsed" />
        </Grid>

        <DataTemplate.Triggers>
          <DataTrigger Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBoxItem}, Path=IsSelected}" 
                       Value="True">
            <Setter TargetName="AutoscaleTextBlock" 
                    Property="Visibility"
                    Value="Collapsed" />
            <Setter TargetName="MarqueeTextBlock" 
                    Property="Visibility"
                    Value="Visible" />
          </DataTrigger>
        </DataTemplate.Triggers>
      </DataTemplate>
    </AutoscrollListBox.ItemTemplate>
  </AutoscrollListBox>
</Grid>
...