窗口手机8.1的ComboBox在选项数量较多时会显示多个选项被选择。

11
当ComboBox中的选项更多时,ComboBox会显示ListPickerFlyout。如果我选择第一个并向下滚动,将会显示多个项目被选中。但是,ComboBox的SelectedItem将是我所选择的那个项目。我修改了ListPickerFlyout的样式,并关闭了ListView的虚拟化。如果这样做,ListView将无法保留SelectedItem。这是ComboBox的错误吗?是否有解决此问题的方法?
这是我修改的样式。
 <DataTemplate x:Key="ListPickerFlyoutPresenterContentTemplate" >
                <ListView VirtualizingStackPanel.VirtualizationMode="Recycling" SelectionMode="Single"   > 
                    <!--<ListView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel/>
                        </ItemsPanelTemplate>
                    </ListView.ItemsPanel>-->
                    <ListView.ItemContainerStyle>

                           <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="ListViewItem">
                                        <Border x:Name="OuterContainer" RenderTransformOrigin="0.5,0.5">
                                            <VisualStateManager.VisualStateGroups>
                                                <VisualStateGroup x:Name="CommonStates">
                                                    <VisualState x:Name="Normal" />
                                                    <VisualState x:Name="Pressed"/>

                                                    <VisualState x:Name="Disabled"/>
                                                                                                                                                        <VisualStateGroup.Transitions>
                                                        <VisualTransition From="Pressed" To="Normal" />


                                                    </VisualStateGroup.Transitions>
                                                </VisualStateGroup>
                                                <VisualStateGroup x:Name="SelectionStates">
                                                    <VisualState x:Name="Unselected" />
                                                    <VisualState x:Name="Selected">
                                                        <Storyboard>
                                                            <DoubleAnimation Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Opacity" Duration="0" To="1" />
                                                            <DoubleAnimation Storyboard.TargetName="SelectedCheckMark" Storyboard.TargetProperty="Opacity" Duration="0" To="1" />
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Foreground" Duration="0">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Green" />
                                                            </ObjectAnimationUsingKeyFrames>
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentBorder" Storyboard.TargetProperty="Background">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListPickerFlyoutPresenterSelectedItemBackgroundThemeBrush}" />
                                                            </ObjectAnimationUsingKeyFrames>
                                                        </Storyboard>
                                                    </VisualState>
                                                    <VisualState x:Name="SelectedUnfocused">
                                                        <Storyboard>
                                                            <DoubleAnimation Storyboard.TargetName="CheckGlyph" Storyboard.TargetProperty="Opacity" Duration="0" To="1" />
                                                            <DoubleAnimation Storyboard.TargetName="SelectedCheckMark" Storyboard.TargetProperty="Opacity" Duration="0" To="1" />
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Foreground" Duration="0">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Green" />
                                                            </ObjectAnimationUsingKeyFrames>
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentBorder" Storyboard.TargetProperty="Background">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListPickerFlyoutPresenterSelectedItemBackgroundThemeBrush}" />
                                                            </ObjectAnimationUsingKeyFrames>
                                                        </Storyboard>
                                                    </VisualState>
                                                </VisualStateGroup>
                                                <VisualStateGroup x:Name="DataVirtualizationStates">
                                                    <VisualState x:Name="DataAvailable" />
                                                    <VisualState x:Name="DataPlaceholder">

                                                    </VisualState>
                                                </VisualStateGroup>
                                                <VisualStateGroup x:Name="MultiSelectStates">
                                                    <VisualState x:Name="NoMultiSelect" />
                                                    <VisualState x:Name="ListMultiSelect">

                                                    </VisualState>
                                                    <VisualState x:Name="GridMultiSelect" />
                                                    <VisualStateGroup.Transitions>
                                                        <VisualTransition From="ListMultiSelect" To="NoMultiSelect" GeneratedDuration="0:0:0.15" />
                                                        <VisualTransition From="NoMultiSelect" To="ListMultiSelect" GeneratedDuration="0:0:0.15" />
                                                    </VisualStateGroup.Transitions>
                                                </VisualStateGroup>
                                                <!--<VisualStateGroup x:Name="HighlightStates">
                                                    <VisualState x:Name="NoHighlight" />
                                                    <VisualState x:Name="Highlighted">
                                                        <Storyboard>
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="contentPresenter" Storyboard.TargetProperty="Foreground" Duration="0">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
                                                            </ObjectAnimationUsingKeyFrames>
                                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentBorder" Storyboard.TargetProperty="Background">
                                                                <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource ListPickerFlyoutPresenterSelectedItemBackgroundThemeBrush}" />
                                                            </ObjectAnimationUsingKeyFrames>
                                                        </Storyboard>
                                                    </VisualState>
                                                </VisualStateGroup>-->
                                            </VisualStateManager.VisualStateGroups>

                                                            <Grid>
                                                                <ContentPresenter x:Name="contentPresenter"
                                        ContentTransitions="{TemplateBinding ContentTransitions}"
                                        ContentTemplate="{TemplateBinding ContentTemplate}"
                                        Content="{TemplateBinding Content}"
                                        HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                        Margin="{TemplateBinding Padding}"
                                        Style="{ThemeResource FlyoutPickerListViewItemContentPresenterStyle}" />
                                                                 <!--The 'Xg' text simulates the amount of space one line of text will occupy.
                                            In the DataPlaceholder state, the Content is not loaded yet so we
                                            approximate the size of the item using placeholder text.--> 
                                                                <TextBlock x:Name="PlaceholderTextBlock"
                                 Opacity="0"
                                 Text="Xg"
                                 Foreground="{x:Null}"
                                 Margin="{TemplateBinding Padding}"
                                 IsHitTestVisible="False"
                                 AutomationProperties.AccessibilityView="Raw"/>
                                                                <Rectangle x:Name="PlaceholderRect"
                                 Visibility="Collapsed"
                                 Fill="{ThemeResource FlyoutBackgroundThemeBrush}"
                                 IsHitTestVisible="False" />
                                                            </Grid>
                                                        </Border>
                                                    </Border>
                                                </Border>
                                                <Border x:Name="SelectedBorder"
                      IsHitTestVisible="False"
                      Opacity="0"
                      BorderBrush="{ThemeResource ListViewItemSelectedBackgroundThemeBrush}"
                      BorderThickness="{ThemeResource GridViewItemMultiselectBorderThickness}">
                                                    <Grid x:Name="SelectedCheckMark"
                      Opacity="0"
                      Height="34"
                      Width="34"
                      HorizontalAlignment="Right"
                      VerticalAlignment="Top">
                                                        <Path x:Name="SelectedEarmark" Data="M0,0 L40,0 L40,40 z"
                        Fill="{ThemeResource ListViewItemSelectedBackgroundThemeBrush}"
                        Stretch="Fill" />
                                                        <Path x:Name="SelectedGlyph"
                        Data="M0,123 L39,93 L124,164 L256,18 L295,49 L124,240 z"
                        Fill="{ThemeResource ListViewItemCheckThemeBrush}"
                        Height="14.5"
                        Stretch="Fill"
                        Width="17"
                        HorizontalAlignment="Right"
                        Margin="0,1,1,0"
                        VerticalAlignment="Top"
                        FlowDirection="LeftToRight" />
                                                    </Grid>
                                                </Border>
                                            </Grid>
                                        </Border>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>

                    </ListView.ItemContainerStyle>
                    <ListView.Footer>
                        <Border Height="{ThemeResource ListPickerFlyoutFooterThemeHeight}" Width="1" />
                    </ListView.Footer>                     
                </ListView>
            </DataTemplate>
当我选择某个项目时,它不会进入选定的可视状态,实际上它会进入高亮的可视状态,并且当我关闭虚拟化时,先前的高亮状态不会被保留。

如果我不编辑任何样式,组合框的实际行为是:从列表选择器中选择一个项目,然后再次进入列表选择器并向下滚动,会显示多个已选择的项目(仅当组合框有许多项目时才会发生这种情况)。 - Archana
当我选择某个项目时,它不会进入所选的可视状态,实际上它会进入突出显示的可视状态,当我关闭虚拟化时,之前突出显示的状态不会被保留。当我编辑ListPickerFlyoutPresenterContentTemplate时,我发现当列表选择器中的项目被点击(选定)时,它总是进入突出显示的可视状态而不是所选的可视状态。 - Archana
1
@user2354187 当项目数量更多时,具体是多少? - Mangesh
当项目数量为39时,我遇到了这个问题。具体的数字我不清楚。 - Archana
1个回答

1
问题出在listviewitems的虚拟化上(这是控件中的一个bug)。
解决方法是设置listview的itemsstackpanel的CacheLength属性。
代码如下:
<DataTemplate x:Key="ListPickerFlyoutPresenterContentTemplate">
    <ListView ItemContainerStyle="{StaticResource ListPickerFlyoutPresenterItemStyle}">
       <ListView.ItemsPanel>
           <ItemsPanelTemplate>
                <ItemsStackPanel  CacheLength="10" />
            </ItemsPanelTemplate>
        </ListView.ItemsPanel>

        <ListView.Footer>
            <Border Height="{ThemeResource ListPickerFlyoutFooterThemeHeight}"
                    Width="1" />
        </ListView.Footer>
    </ListView>
</DataTemplate>

该解决方案在项目实现后的几秒钟内才会移除选择高亮。如果该方案不可行,则需要覆盖listview的PrepareContainerForItemOverride方法。

检查了您的代码。如您所说,高亮将在几秒钟内出现。 - Archana
你能帮我解决这个问题吗?链接 - Archana

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接