子项控件未显示图片

时间:2018-06-29 10:35:11

标签: wpf xaml itemscontrol collectionviewsource

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

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

我现在将ListView的源更改为CollectionViewSource,以对Items进行分组。即使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>

1 个答案:

答案 0 :(得分:1)

不确定为什么正确设置IsAsync=True不能与CollectionViewSource一起使用,但是无论如何都没有意义。您的PicturePreview的getter根本不需要花费任何时间,因此不需要异步调用它。

因此只需从绑定中删除IsAsync

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

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