Стиль для ListViewItem не работает

225
25 февраля 2019, 21:20

Когда выделяю item из списка, border не остается на этом item. Не могу понять почему. При наведении на любой из items, border показывается.

    <Style TargetType="ListViewItem" x:Key="ListViewItemObject">
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
    <Setter Property="TabNavigation" Value="Local"/>
    <Setter Property="IsHoldingEnabled" Value="True"/>
    <Setter Property="Margin" Value="20,8"/>
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
    <Setter Property="VerticalContentAlignment" Value="Stretch"/>
    <Setter Property="MinWidth" Value="304"/>
    <Setter Property="MinHeight" Value="100"/>
    <Setter Property="UseSystemFocusVisuals" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <Grid x:Name="ContentBorder"
          Background="{TemplateBinding Background}"
          BorderBrush="{StaticResource WaterBlueSolidColorBrush}"
          BorderThickness="0"
          MinWidth="{TemplateBinding MinWidth}"
          MinHeight="{TemplateBinding MinHeight}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="PointerOver">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="BorderBackground"
                             Storyboard.TargetProperty="Opacity"
                             Duration="0"
                             To="1"/>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListLowBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MultiSelectSquare" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MultiSelectSquare" Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource WaterBlueSolidColorBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListMediumBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MultiSelectSquare" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MultiSelectSquare" Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource YellowColor}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <PointerDownThemeAnimation TargetName="ContentPresenter" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Selected">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentBorder" Storyboard.TargetProperty="BorderThickness">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="3"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenterGrid" Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource BlackSolidColorBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource DarkerGrayTextBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MultiSelectSquare" Storyboard.TargetProperty="Visibility">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MultiSelectSquare" Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource BlackColor}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="PointerOverSelected">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentBorder" Storyboard.TargetProperty="BorderThickness">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="3"/>
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightListAccentMediumBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <PointerUpThemeAnimation Storyboard.TargetName="ContentPresenter" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="PressedSelected">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="MultiSelectCheck"
                             Storyboard.TargetProperty="Opacity"
                             Duration="0:0:0"
                             To="1"/>
                                    <DoubleAnimation Storyboard.TargetName="BorderBackground"
                             Storyboard.TargetProperty="Opacity"
                             Duration="0"
                             To="1"/>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="BorderBackground" Storyboard.TargetProperty="Fill">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource WaterBlueSolidColorBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <PointerDownThemeAnimation TargetName="ContentPresenter" />
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Rectangle x:Name="BorderBackground"
                IsHitTestVisible="False"
                Fill="{ThemeResource SystemControlHighlightListAccentLowBrush}"
                Opacity="0"
                Control.IsTemplateFocusTarget="True"/>
                    <Border x:Name="MultiSelectSquare"
                                BorderBrush="{ThemeResource WaterBlueSolidColorBrush}"
                                BorderThickness="3"
                                Visibility="Collapsed"/>
                    <Grid x:Name="ContentPresenterGrid"
                          Background="Transparent"
                          Margin="0,0,0,0">
                        <Grid.RenderTransform>
                            <TranslateTransform x:Name="ContentPresenterTranslateTransform"/>
                        </Grid.RenderTransform>
                        <ContentPresenter x:Name="ContentPresenter"
                        ContentTransitions="{TemplateBinding ContentTransitions}"
                        ContentTemplate="{TemplateBinding ContentTemplate}"
                        Content="{TemplateBinding Content}"
                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
Answer 1

В описании листа есть SelectionMode. Должен быть SelectionMode="Single". У меня он был None.

        <ListView
        x:Name="ModelsListView"
        Grid.Row="1"
        ScrollViewer.VerticalScrollBarVisibility="Auto"
        SelectionMode="Single"
        IsItemClickEnabled="True"
        ItemContainerStyle="{StaticResource ListViewItemObject}"
        ItemsSource="{x:Bind ViewModel.Models, Mode=OneWay}"
        ItemClick="ModelsListView_ItemClick" SelectionChanged="ModelsListView_SelectionChanged">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="models:ModelItem">
                <Grid Background="{StaticResource LightTransparentWhiteSolidColorBrush}">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock 
                        Text="{x:Bind Name, Mode=OneWay}"
                        VerticalAlignment="Center"
                        FontSize="17"
                        Margin="20,0"
                        Foreground="Black"
                        FontWeight="Bold"
                        TextWrapping="WrapWholeWords"
                        HorizontalAlignment="Left"
                        Width="1200"/>
                    <TextBlock 
                        Visibility="{x:Bind IsDefault, Converter={StaticResource BooleanToVisibilityConverter}, Mode=OneWay}"
                        Text="Default"
                        HorizontalAlignment="Right"
                        VerticalAlignment="Center"
                        FontSize="14"
                        Foreground="Black"
                        FontWeight="Bold"
                        Width="75"/>
                    <Button Grid.Column="1"
                            Foreground="Black"
                            VerticalAlignment="Center"
                            Background="Transparent"
                            BorderThickness="0"
                            Margin="0,12">
                        <TextBlock 
                            Text="&#xE10C;" 
                            FontFamily="Segoe MDL2 Assets" 
                            FontSize="26"
                            FontWeight="ExtraBold"
                            RenderTransformOrigin="0.5,0.5">
                            <TextBlock.RenderTransform>
                                <CompositeTransform Rotation="90"/>
                            </TextBlock.RenderTransform>
                        </TextBlock>
             </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
READ ALSO
Экранирование слэшей

Экранирование слэшей

как стоит изменить код, чтобы он мог понимать, где идёт более 2 слэшей подряд?

154
Как создать консольное приложение .Net Standard?

Как создать консольное приложение .Net Standard?

Внимание! Это перевод вопроса Console App (Net Standard) not listed

141
Xamarin Forms C# Как убрать скрол у ListView?

Xamarin Forms C# Как убрать скрол у ListView?

Начинаю вникать в xamarinРешил написать простенький проект а-ля заметочника

145
Раскодировать ответ

Раскодировать ответ

С помощью класса HttpClient конекчусь к некоему APIПолучаю такой ответ:

151