Tuesday, 20 August 2013

Is there a way to make this image binding more performant?

Is there a way to make this image binding more performant?

I am binding a list of 500 odd nicks to a list with a status image for
each. The scrolling of the list is painfully slow, and so is flicking
between tabs with different lists.
This is all caused by my recent change in which I added these images.
Is there a way to speed it up?
My bitmaps (very small 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" />
My List :
<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>
My converter :
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();
}
}

No comments:

Post a Comment