有没有办法让这个图像绑定更高效?

时间:2013-08-20 16:40:25

标签: wpf image data-binding datatemplate itemtemplate

我将500个奇数刻痕的列表绑定到列表中,每个列表都有状态图像。滚动列表的速度非常慢,因此在具有不同列表的选项卡之间轻弹。

这都是由于我最近更改了这些图片所引起的。

有没有办法加快速度?

我的位图(非常小的16 * 16):

<BitmapImage x:Key="ActiveIcon" UriSource="/WPFClient;component/Images/active.png" />
<BitmapImage x:Key="IdleIcon" UriSource="/WPFClient;component/Images/idle.png" />
<BitmapImage x:Key="AwayIcon" UriSource="/WPFClient;component/Images/away.png" />
<BitmapImage x:Key="UnknownIcon" UriSource="/WPFClient;component/Images/unknown.png" />

我的列表:

<ListBox ItemsSource="{Binding Users}">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <DockPanel>
         <Image Source="{Binding Status, Converter={StaticResource UserStatusToIconConverter}}" Height="16" Width="16" Margin="0,0,5,0" />
         <TextBlock Text="{Binding Nick}" />
       </DockPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

我的转换器:

public class UserStatusToIconConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        string userStatus = value.ToString();
        string iconName = "UnknownIcon";

        switch (userStatus)
        {
            case "Active":
                iconName = "ActiveIcon";
                break;
            case "Idle":
                iconName = "IdleIcon";
                break;
            case "Away":
                iconName = "AwayIcon";
                break;
        }

        return iconName;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

注意:代码也不能正常工作,实际上没有显示图像。但是我认为这是一个小细节。

2 个答案:

答案 0 :(得分:1)

尝试使用VirtualizationStackPanel会给出一些改进

<ListBox VirtualizingStackPanel.IsVirtualizing="True"
                 VirtualizingStackPanel.VirtualizationMode="Recycling">

</ListBox>
框架4.5中的

VirtualizingPanel可用,设置VirtualizingPanel.ScrollUnit =“Item”将提供良好的性能提升

答案 1 :(得分:0)

解决这个问题的最简单方法是将转换器更改为:

public class UserStatusToIconConverter : IValueConverter
{
    private static readonly Uri ActiveIcon = new Uri("pack://application:,,,/WPFClient;component/Images/active.png");
    private static readonly Uri IdleIcon = new Uri("pack://application:,,,/WPFClient;component/Images/idle.png");
    private static readonly Uri AwayIcon = new Uri("pack://application:,,,/WPFClient;component/Images/away.png");
    private static readonly Uri UnknownIcon = new Uri("pack://application:,,,/WPFClient;component/Images/unknown.png");

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var userStatus = value.ToString();
        switch (userStatus)
        {
            case "Active":
                return ActiveIcon;
            case "Idle":
                return IdleIcon;
            case "Away":
                return AwayIcon;
            default:
                return UnknownIcon;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

减速是由于绑定不当引起的。