WPF Itemscontrol shows duplicate entries

Deadly 提交于 2019-12-13 03:38:33

问题


I'm using an ItemControl to display a list of strings (like a suggestion-list). My problem is that it sometimes duplicates one entry..

I've tried to disable virtualization without success...

this is my xaml-code:

<ItemsControl ItemsSource="{Binding ResultList}">              
<ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
        <VirtualizingStackPanel Orientation="Vertical" IsVirtualizing="False" IsContainerVirtualizable="False" VirtualizationMode="Standard"></VirtualizingStackPanel>
    </ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Button>
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <TextBlock Text="{Binding DisplayName}"></TextBlock>
                </ControlTemplate>
            </Button.Template>                                       
        </Button>
    </DataTemplate>
</ItemsControl.ItemTemplate>

as you can see there are 3 strings shown but I only have 2 in my binding-ResultList...(Ergebnisse 2 is bound to the ResultList.Count)

ResultList is of type ObservableCollection().


回答1:


I finally found a solution for that problem.

These duplicate items are only shown when I put my listbox in a popup (to simulate a suggestion-field).

The only thing I had to do was adding this line of code after I changed the entries of ResultList to Refresh the list.

CollectionViewSource.GetDefaultView(field.ResultList).Refresh();


来源:https://stackoverflow.com/questions/47773236/wpf-itemscontrol-shows-duplicate-entries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!