xamlwin-universal-appuwpuwp-xaml

Listview has multiple selection even setting selection mode single?


I have a list view and but my listview has multiple selection even if selection mode is single?I have posted the properties set on listview and the itemcontainerstyle.

     <ListView SelectionMode="Single" Tapped="UIElementTapped"
                    ItemsSource="{Binding Path=ListTimeFrame}"
                    SelectedItem="{Binding Path=SelectedTimeFrame, Mode=TwoWay}"
                    ItemContainerStyle="{StaticResource TimeFrameListViewItemContainerStyle}"
                    Padding="0"
                    Margin="0">

and itemcontainer style as follows

<Style x:Key="TimeFrameListViewItemContainerStyle" TargetType="ListViewItem">
    <Setter Property="Padding" Value="4,1" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListViewItem">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}"
                       >
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualState x:Name="Unselected" />
                            <VisualState x:Name="Selected">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="SelectingGlyph"
                                            Storyboard.TargetProperty="Opacity" Duration="0" To="1" />
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter"
                                            Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                Value="{StaticResource DefaultTextBlueColor}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="SelectedUnfocused" />
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Grid Margin="{TemplateBinding Padding}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="Auto" />
                            <ColumnDefinition />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="&#xE10B;" FontFamily="Segoe UI Symbol"
                                Foreground="{StaticResource DefaultTextBlueColor}" x:Name="SelectingGlyph"
                                Opacity="0" FontSize="20" LineStackingStrategy="BlockLineHeight" />
                        <ContentPresenter x:Name="contentPresenter" Grid.Column="1"
                                ContentTransitions="{TemplateBinding ContentTransitions}"
                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                Content="{TemplateBinding Content}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Grid>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Solution

  • Listview has multiple selection even setting selection mode single

    This is because you are lacking a Normal visual state. You set the selected visual state in the ListViewItem style, but you don't give the Normal state. Unselected state in ListViewItem is the Normal state. So add the following visual state in your SelectionStates visual state group.

    <VisualState x:Name="Normal">
        <Storyboard>
            <PointerUpThemeAnimation Storyboard.TargetName="contentPresenter" />
        </Storyboard>
    </VisualState>
    

    All the visual state code looks like:

     <VisualStateManager.VisualStateGroups>
         <VisualStateGroup x:Name="SelectionStates">
             <VisualState x:Name="Normal">
                 <Storyboard>
                     <PointerUpThemeAnimation Storyboard.TargetName="contentPresenter" />
                 </Storyboard>
             </VisualState>        
             <VisualState x:Name="Selected">
                 <Storyboard>
                     <DoubleAnimation Duration="0"
                                      Storyboard.TargetName="SelectingGlyph"
                                      Storyboard.TargetProperty="Opacity"
                                      To="1" />
                     <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Foreground">
                         <DiscreteObjectKeyFrame KeyTime="0" Value="Blue" />
                     </ObjectAnimationUsingKeyFrames>
                 </Storyboard>
             </VisualState>
             <VisualState x:Name="SelectedUnfocused" />
         </VisualStateGroup>
     </VisualStateManager.VisualStateGroups>