子项控件不显示图片

Sub ItemsControl not showing pictures

我有一个 ListView,其中的项目包含一个带有一些 TextBlock 的嵌套 ItemsControl 和一个图像控件。

    private string _PicturePreview;
    public string PicturePreview { get => _PicturePreview; set => _PicturePreview = value; }

我现在已将 ListView 的源更改为 CollectionViewSource 以对项目进行分组。一切都很好地呈现,甚至 ListView 中的图片,但由于某种原因,嵌套的 ItemsControl 中的图像不再加载。当我切换回没有 CollectionViewSource 的直接绑定时,图像正在正确加载。

有人知道为什么吗?

    <UserControl.Resources>

    <CollectionViewSource x:Key="Sales" Source="{Binding Bestellungen}" >

        <CollectionViewSource.GroupDescriptions>

            <PropertyGroupDescription PropertyName="OrderTyp"/>

        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

</UserControl.Resources>

<ListView Name="OrdersListView" Grid.Row="1" 
              ItemsSource="{Binding Source={StaticResource Sales}}"            
              SelectedItem="{Binding SelectedBestellung}" 
              AlternationCount="2" 
              IsSynchronizedWithCurrentItem="True" 
              ScrollViewer.CanContentScroll="False"
              >

        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Expander IsExpanded="True" >

                                        <Expander.Header>

                                            <DockPanel Background="#162270"  Width="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Expander}}, Path=ActualWidth}" >
                                                <TextBlock FontWeight="Bold" FontSize="24" Foreground="White" Text="{Binding Path=Name}" Margin="10" />


                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter />
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Style.Triggers>
                    <Trigger Property="ItemsControl.AlternationIndex"  Value="0">
                        <Setter Property="Background" Value="White" />
                    </Trigger>
                    <Trigger Property="ItemsControl.AlternationIndex"  Value="1">
                        <Setter Property="Background" Value="#f2f2f2" />

                    </Trigger>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="BorderThickness" Value="2"/>
                        <Setter Property="BorderBrush" Value="Red"/>
                    </Trigger>
                </Style.Triggers>

                <Setter Property="FontSize" Value="16"/>
                <Setter Property="BorderBrush" Value="Black"></Setter>
                <Setter Property="BorderThickness" Value="0,0,0,2"></Setter>
                <Setter Property="VerticalContentAlignment" Value="Top"/>
                <Setter Property="FrameworkElement.Margin" Value="5"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.View>
            <GridView>
                <GridView.Columns>

                    <GridViewColumn Header="Order">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Belegnummer}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>


                    <GridViewColumn Header="Positionen" Width="800">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ItemsControl ItemsSource="{Binding Positionen}" >
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <Grid Margin="0,0,10,5">

                                                <Grid.ColumnDefinitions>
                                                    <ColumnDefinition Width="50" />
                                                    <ColumnDefinition Width="auto" />
                                                </Grid.ColumnDefinitions>

                                                <Image Width="50" Height="50" Source="{Binding PicturePreview, IsAsync=True}"/>
                                                <StackPanel Grid.Column="1" Orientation="Vertical">

                                                    <DockPanel>
                                                        <TextBlock Text="Stück: " FontWeight="Bold" FontSize="18"/>
                                                        <TextBlock Text="{Binding Stück, StringFormat=N0}" FontWeight="Bold" FontSize="18"/>
                                                    </DockPanel>

                                                </StackPanel>

                                            </Grid>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>

                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                    <GridViewColumn >
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Vertical">
                                    <Image Source="{Binding VersandartNummer, Converter={StaticResource StringToBitmapConverter}}" MaxHeight="100" MaxWidth="100"/>

                                </StackPanel>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>


                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

不确定为什么设置 IsAsync=True 不能与 CollectionViewSource 结合使用,但无论如何设置它是没有意义的。你的PicturePreview的getter根本不做任何耗时的事情,所以没必要异步调用。

所以只需从绑定中删除 IsAsync

 <Image ... Source="{Binding PicturePreview}"/>

请注意,从字符串到 ImageSource 的内置类型转换会创建一个已异步加载的 BitmapFrame,以防 URL 字符串引用 Web 资源。