简体   繁体   English

调整选定的ListViewItem TextBlock样式

[英]Adjust Selected ListViewItem TextBlock Style

I am having a strange issue. 我有一个奇怪的问题。 I am not sure if this is a bug, or if I am simply misunderstanding something as I am fairly new to WPF (Probably the latter). 我不确定这是否是错误,还是我只是对WPF(可能是后者)不熟悉而只是误解了什么。

In my project, I have a ListView that displays items in a similar fashion to that seen of Windows Explorer using Icon view. 在我的项目中,我有一个ListView ,它以与使用图标视图的Windows资源管理器类似的方式显示项目。 I have outlined a control template that consists of a an Image element, and a TextBlock element below it. 我概述了一个控件模板,该模板由一个Image元素和一个在其下面的TextBlock元素组成。 My goal is to adjust the maximum height of the TextBlock when the ListViewItem is selected. 我的目标是在选择ListViewItem时调整TextBlock的最大高度。 This is so the name of the Items will adjust from being trimmed with an ellipsis to showing the full name of the item. 这样一来,项目的名称将调整为从省略号修剪到显示项目的全名。

When the item is selected however, instead of adjusting the maximum height of only the selected item's TextBlock , it adjusts all TextBlock s for each item whether it is actively selected or not. 但是,当选中该项目时,它不会调整选中项目的TextBlock的最大高度,而是会调整每个项目的所有TextBlock ,无论是否处于活动状态。

I have researched for an answer, but have not found anything similar to this particular issue. 我已经研究过答案,但是没有发现与这个特定问题类似的东西。 This link is a similar concept, but without my problem. 此链接是一个类似的概念,但是没有问题。

WPF - ListView Item on Selected change Font size WPF-ListView项目在选定的更改上的字体大小

Some of my other methods have consisted of one ControlTemplate with triggers for the style changes, or ItemContainerStyle instead of explicitly a ControlTemplate , which all seemed to give the same undesired result. 我的其他一些方法由一个带有样式更改触发器的ControlTemplate ,或者由ItemContainerStyle而不是显式ControlTemplate ,它们似乎都给出了相同的不良结果。

How can I achieve this functionality? 如何实现此功能? Is it possible with ControlTemplate ? ControlTemplate是否可能?

Here is some of my XAML code: 这是我的一些XAML代码:

ListView 列表显示

<ListView x:Name="ItemViewer">
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Margin="10"/>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Setter Property="Margin" Value="10"/>
            <Setter Property="Template" Value="{StaticResource ListViewItemNormal}"/>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsSelected" Value="True"/>
                        <Condition Property="Selector.IsSelectionActive" Value="True"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="Template" Value="{StaticResource ListViewItemSelected}"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

ControlTemplates CONTROLTEMPLATES

<ControlTemplate x:Key="ListViewItemNormal" TargetType="{x:Type ListViewItem}">
    <Border x:Name="ItemBoxBorder" Background="Transparent">
        <Grid HorizontalAlignment="Left" MinHeight="90"
              Margin="5"
              MaxWidth="90"
              Width="90"
              x:Name="ItemBox">
            <StackPanel>
                <Image HorizontalAlignment="Center"
                       VerticalAlignment="Top"
                       Source="{StaticResource NewImage}"
                       Width="64" Height="64"/>
                <TextBlock x:Name="ItemDescription"
                           Text="{Binding Path=Name}"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Bottom"
                           TextWrapping="Wrap"
                           MaxWidth="90"
                           MaxHeight="30"
                           TextTrimming="CharacterEllipsis"/>
            </StackPanel>
            <Grid.ToolTip>
                <ToolTip Content="{Binding Path=Name}"/>
            </Grid.ToolTip>
        </Grid>
    </Border>
    <ControlTemplate.Triggers>
        <MultiTrigger>
            <MultiTrigger.Conditions>
                <Condition Property="IsSelected" Value="False"/>
                <Condition Property="IsMouseOver" Value="True"/>
            </MultiTrigger.Conditions>
            <Setter TargetName="ItemBoxBorder"  Property="Background" Value="{StaticResource HighlightMouseHoverColorBrush}"/>
        </MultiTrigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

<ControlTemplate x:Key="ListViewItemSelected" TargetType="{x:Type ListViewItem}">
    <Border x:Name="ItemBoxBorder" Background="{StaticResource SelectedItemBrush}"
            BorderBrush="{StaticResource HighlightBorderColorBrush}"
            BorderThickness="1">
        <Grid HorizontalAlignment="Left" MinHeight="90"
              Margin="5"
              MaxWidth="90"
              Width="90"
              x:Name="ItemBox">
            <StackPanel>
                <Image HorizontalAlignment="Center"
                       VerticalAlignment="Top"
                       Source="{StaticResource NewImage}"
                       Width="64" Height="64"/>
                <TextBlock x:Name="ItemDescription"
                           Text="{Binding Path=Name}"
                           HorizontalAlignment="Center"
                           VerticalAlignment="Bottom"
                           TextWrapping="Wrap"
                           MaxWidth="90"
                           MaxHeight="125"
                           TextTrimming="CharacterEllipsis"/>
            </StackPanel>
            <Grid.ToolTip>
                <ToolTip Content="{Binding Path=Name}"/>
            </Grid.ToolTip>
        </Grid>
    </Border>
</ControlTemplate>

EDIT 编辑

Here is an example of the issue with ms_dos's implementation. 这是ms_dos的实现问题的示例。

This image shows I have the item with a short description selected. 此图显示我选择了简短说明的项目。 This is the height all items should remain if they are not selected. 这是所有项目(如果未选中)应保留的高度。

In this image, you'll see the item with the long description is selected. 在此图像中,您将看到带有长说明的项目被选中。 However, both items extend their height, but only the selected one should grow. 但是,两个项目都可以扩展其高度,但是只有所选项目可以增长。

You don't need two separate control templates for this. 您不需要两个单独的控制模板。 Just take the ItemContainerStyle property of the ListView and use a control template like this: 只需采用ListViewItemContainerStyle属性,并使用如下控件模板:

<ListView x:Name="ItemViewer">
        <ListView.ItemsPanel>
            <ItemsPanelTemplate>
                <WrapPanel Margin="10" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>

        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListViewItem">
                            <Border x:Name="ItemBoxBorder"
                                    Background="Green"
                                    BorderThickness="1"
                                    VerticalAlignment="Top">
                                <Grid HorizontalAlignment="Left"
                                      MinHeight="90"
                                      Margin="5"
                                      MaxWidth="90"
                                      Width="90"
                                      x:Name="ItemBox">
                                    <StackPanel>
                                        <Image HorizontalAlignment="Center"
                                               VerticalAlignment="Top"
                                               Width="64"
                                               Height="64" />
                                        <TextBlock x:Name="ItemDescription"
                                                   HorizontalAlignment="Center"
                                                   VerticalAlignment="Bottom"
                                                   TextWrapping="Wrap"
                                                   Text="{Binding}"
                                                   MaxWidth="90"
                                                   MaxHeight="125"
                                                   TextTrimming="CharacterEllipsis" />
                                    </StackPanel>
                                    <Grid.ToolTip>
                                        <ToolTip Content="{Binding Path=Name}" />
                                    </Grid.ToolTip>
                                </Grid>
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected"
                                         Value="true">
                                    <Setter TargetName="ItemBoxBorder"
                                            Property="Background"
                                            Value="Red" />
                                    <Setter TargetName="ItemDescription"
                                            Property="TextElement.FontSize"
                                            Value="20" />
                                    <Setter TargetName="ItemBoxBorder"
                                            Property="Height"
                                            Value="135" />
                                </Trigger>
                                <Trigger Property="IsSelected"
                                         Value="false">
                                    <Setter TargetName="ItemBoxBorder"
                                            Property="Height"
                                            Value="90" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ListView.ItemContainerStyle>
</ListView>

Take a look at the ControlTemplate.Triggers section. 看一下ControlTemplate.Triggers部分。 Basically you use the Trigger property to control which property the trigger should handle... in our case the IsSelected from the ListView. 基本上,您使用Trigger属性来控制触发器应处理的属性...在本例中,是ListView中的IsSelected And with the Setter you define the property and the value for the changes of the ListViewItem control. 并使用Setter定义ListViewItem控件的更改的属性和值。 The TargetName of the Setter refers to the x:Name of the control which you define in the ControlTemplate section. TargetName了的Setter是指x:Name您在定义控制ControlTemplate部分。

I hope this example helps you with your problem! 我希望这个例子可以帮助您解决问题!

Greets ms_dos 向ms_dos致意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM