0

As a self-set himself the task of writing the messenger (like Telegram, Viber, etc). To display the messages in the chat, I use ListView with FlowDocument as ItemTemplate . This control was chosen because:
1) It is necessary to display images (emoticons);
2) Hyperlinks;
3) It should be possible to select text to copy.

But the application consumes lots of memory, a feeling that FlowDocument not deleted from the memory.

It's my View:

            <CollectionViewSource x:Key="ItemListViewSource"
                              IsLiveSortingRequested="True"
                              Source="{Binding RelativeSource={RelativeSource AncestorType=UserControl},
                                               Path=Tag.Messages}">
            <CollectionViewSource.SortDescriptions>
                <scm:SortDescription PropertyName="date" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

    <Style x:Key="lvItemStyle" TargetType="{x:Type ListViewItem}">
        <Setter Property="Background" Value="White" />
        <Setter Property="Padding" Value="0" />
        <Setter Property="Focusable" Value="False" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border x:Name="Bd"
                            Background="{TemplateBinding Background}"
                            BorderThickness="0"
                            Padding="{TemplateBinding Padding}">
                        <ContentPresenter HorizontalAlignment="Stretch" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Style.Triggers>
            <Trigger Property="IsSelected" Value="True">
                <Setter Property="Background" Value="LightGray" />
            </Trigger>
        </Style.Triggers>
    </Style>


<Style x:Key="lvStyle" TargetType="{x:Type ListView}">
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Background" Value="White" />
        <Setter Property="SelectionMode" Value="Multiple" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="True" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
        <Setter Property="VirtualizingPanel.IsVirtualizing" Value="True" />
        <Setter Property="VirtualizingPanel.ScrollUnit" Value="Pixel" />
        <Setter Property="Margin" Value="0" />
        <Setter Property="ItemContainerStyle" Value="{StaticResource lvItemStyle}" />
        <Setter Property="Padding" Value="0" />
    </Style>   


 <DataTemplate x:Key="MessageTemplate">
            <Grid Margin="0,7,0,7">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>

                <Image Grid.Row="0"
                       Grid.RowSpan="2"
                       Grid.Column="0"
                       Width="40"
                       Height="40"
                       Margin="5,0,10,0"
                       VerticalAlignment="Top"
                       Source="{Binding Converter={StaticResource getAvatarForChat},
                                        Path=fromId,
                                        Mode=OneWay,
                                        ConverterParameter=40}">
                    <Image.Clip>
                        <EllipseGeometry Center="20,20"
                                         RadiusX="20"
                                         RadiusY="20" />
                    </Image.Clip>
                </Image>
                <TextBlock Grid.Row="0"
                           Grid.Column="1"
                           Margin="0"
                           VerticalAlignment="Top"
                           FontSize="13"
                           FontWeight="SemiBold"
                           Padding="0"
                           Text="{Binding chatTitle, Mode=OneWay" />
                <TextBlock Grid.Row="0"
                           Grid.Column="2"
                           Margin="10,0,0,0"
                           HorizontalAlignment="Left"
                           VerticalAlignment="Top"
                           FontSize="13"
                           FontWeight="ExtraLight"
                           Padding="0"
                           Text="{Binding Path=date,
                                          Converter={StaticResource dateConverter},
                                          ConverterParameter=HH:mm,
                                          Mode=OneWay}" />

        <FlowDocumentScrollViewer Grid.Row="1"
                                    Grid.Column="1"
                                    Grid.ColumnSpan="2"
                                    Margin="0,3,0,0"
                                    Document="{Binding message,
                                               Converter={StaticResource documentConverter},
                                               Mode=OneWay}"
                                    Padding="0"/>

            </Grid>
    </DataTemplate>


     <ListView x:Name="listView"
                  Grid.Row="0"
                  ItemsSource="{Binding Source={StaticResource ItemListViewSource},
                                        NotifyOnTargetUpdated=True}""
          ItemTemplate="StaticResource MessageTemplate"
                  Style="{StaticResource lvStyle}">
    </ListView>

Please help me understand this problem, and I'm sorry for my english!

  • Possible duplicate of [Virtualizing WrapPanel as ListView's ItemsTemplate](http://stackoverflow.com/questions/32720694/virtualizing-wrappanel-as-listviews-itemstemplate) – Itay Podhajcer Jan 05 '17 at 13:37
  • @ItayPodhajcer I did not change itemspanel, but thank you for reply! – Anton Man'kov Jan 05 '17 at 13:42
  • It might help you get better performance. – Itay Podhajcer Jan 05 '17 at 13:44
  • Hmm, I'll try to use a solution that you suggest. But [Snoop](https://snoopwpf.codeplex.com/) shows that in the Visual Tree a bit, but the memory is not freed. If one uses `TextBlock` everything is fine, but this control does not support selection – Anton Man'kov Jan 05 '17 at 14:41

0 Answers0