【问题标题】:WPF - WrapPanel doesn't update/redraw images contentWPF - WrapPanel 不更新/重绘图像内容
【发布时间】:2014-03-19 09:38:35
【问题描述】:

我正在从一个文件夹中加载图像,并以窗口“大图标”的方式在包装面板中显示它们,这样用户就可以选择/标记他想要的图像。

目前我正在读取图像文件位置,将它们添加到字符串列表中,并将 ItemSource 设置为 ItemsControl,如下所示:

<ItemsControl x:Name="ItemsControlPhotos" Margin="10" >
    <ItemsControl.Template>
        <ControlTemplate>
            <WrapPanel FlowDirection="LeftToRight" IsItemsHost="true">
            </WrapPanel>
        </ControlTemplate>
    </ItemsControl.Template>
    <ItemsControl.ItemTemplate>
        <DataTemplate >
            <Image Source="{Binding}" Width="100" Height="100" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

这似乎工作正常。但是有些图像看起来很奇怪,就好像它们只画了一次。 请参考this video for an example

我尝试使用几种不同的方法手动更新列表:

ItemsControlPhotos.Items.Refresh();

ItemsControlPhotos.InvalidateArrange();

ItemsControlPhotos.InvalidateMeasure();

ItemsControlPhotos.InvalidateVisual();

但没有任何效果,所以我有点迷茫。

非常感谢您的帮助! :)

编辑: 这是完整的窗口标记:

    <Window x:Class="Name.Space"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
        mc:Ignorable="d"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:System="clr-namespace:System;assembly=mscorlib"
        Title="PhotoChooser" 
        WindowState="Maximized"
        Width="1280"
        >
  <Window.Resources>
    <ResourceDictionary>
      <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="../Styles/Styles.xaml" />
      </ResourceDictionary.MergedDictionaries>
      <System:Double x:Key="{x:Static SystemParameters.VerticalScrollBarWidthKey}">40</System:Double>
    </ResourceDictionary>
  </Window.Resources>

  <Border BorderBrush="Black" BorderThickness="1" Background="White" Width="1280">

    <DockPanel>

      <Image Source="..\Images\BGTop.png" DockPanel.Dock="Top" Grid.Row="0" />

      <Grid DockPanel.Dock="Top" Margin="0,-80,0,0" >
        <Grid.RowDefinitions>
          <RowDefinition Height="Auto" />
          <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>

        <ScrollViewer>
          <ItemsControl x:Name="ItemsControlPhotos" Margin="10" >
            <ItemsControl.Template>
              <ControlTemplate>
                <WrapPanel FlowDirection="LeftToRight" IsItemsHost="true">
                </WrapPanel>
              </ControlTemplate>
            </ItemsControl.Template>
            <ItemsControl.ItemTemplate>
              <DataTemplate >
                <Image Source="{Binding}" Width="100" Height="100" />
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
        </ScrollViewer>
      </Grid>

      <Grid DockPanel.Dock="Bottom">
        <Button Height="50" Width="200" Click="ButtonBase_OnClick">Add photos</Button>
        <Image  Source="..\Images\BGBottom.png" Grid.Row="99" VerticalAlignment="Bottom" Margin="0,0,0,0" />
      </Grid>

    </DockPanel>
  </Border>
</Window>

【问题讨论】:

  • 这看起来更像是父容器的问题,什么是父容器?
  • 我已经添加了完整的 windoww xaml。
  • 您说您将“ItemSource 设置为 ItemsControl”。这是你的意思吗? ItemsSource 应该是 ObservableCollection 或包含您的数据项的类似名称。
  • 我有一个字符串列表,所以我将其更改为 ObservableCollection,但它没有帮助。不过谢谢..

标签: c# wpf wrappanel


【解决方案1】:

我并没有真正解决问题,但我在处理项目时找到了解决方案。

我不想加载整个图像,而只想加载一个缩略图,以加快进程。所以我点击了这个链接:http://blogs.msdn.com/b/dditweb/archive/2007/08/22/speeding-up-image-loading-in-wpf-using-thumbnails.aspx

现在问题消失了。 感谢您的贡献! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 2013-07-19
    • 2019-06-02
    • 2017-11-26
    相关资源
    最近更新 更多