.NET Framework Developer Center >
.NET Development Forums
>
Windows Presentation Foundation (WPF)
>
2010: CacheMode for ListViewItem?
2010: CacheMode for ListViewItem?
- Hello,
it just occurs to me that in my problematic ListView (40 items long, scrolling a LOT downward as data drops in on the top - a LOT)... I may actually win a lot by caching the indivisual lines (ListViewItems) with the CacheMode property introduced in 4.0 ;) It would mean after a scroll the line could only be redrawrn from bitmap, not from the elements... may make sense ;)
Setting CacheMode on the complete list is not good, as it scrolls down, so it invalidates completely every time.
Anyone can help me out how to set that?
I dont define a complete style so far - I am more than happy with the standard. I define an ItemCntainerStyle, but it basically sets HorizontalContentAlignment AND has a lot of datatrigger color changes.
Is there a way to access the CacheMode property of the ListViewItem at the ItemContainerStyle? Or how else (yeah, willing to learn) can I do that?
Answers
- I suppose I don't understand the problem here. ListViewItem is a Control, which is a UIElement, so you should be able to set the property just like you would any other. Setting this style as a ListBox's ItemContainerStyle worked fine for me.
<Style TargetType="ListBoxItem" x:Key="ContainerStyle">
<Setter Property="CacheMode" Value="BitmapCache"/>
</Style>- Marked As Answer byJustTom Wednesday, November 04, 2009 8:35 AM
- Unmarked As Answer byJustTom Wednesday, November 04, 2009 8:49 AM
- Proposed As Answer byBrendan Clark - MSFT Wednesday, November 04, 2009 3:02 AM
- Marked As Answer byJustTom Thursday, November 05, 2009 1:18 AM
- If you enable ClearType you'll also want to set SnapsToDevicePixels to true. Snapping the cache to pixels should make everything less blurry, including the text. The example XAML I gave you cheated a bit since we added a TypeConverter that creates a default-valued BitmapCache if you use the shorthand notation I used. The verbose way that allows you to specify all the properties would look like this:
<Style TargetType="ListBoxItem" x:Key="ContainerStyle">
<Setter Property="CacheMode">
<Setter.Value>
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
</Setter.Value>
</Setter>
</Style>- Proposed As Answer byBrendan Clark - MSFT Wednesday, November 04, 2009 8:04 PM
- Marked As Answer byJustTom Thursday, November 05, 2009 1:11 AM
All Replies
- I suppose I don't understand the problem here. ListViewItem is a Control, which is a UIElement, so you should be able to set the property just like you would any other. Setting this style as a ListBox's ItemContainerStyle worked fine for me.
<Style TargetType="ListBoxItem" x:Key="ContainerStyle">
<Setter Property="CacheMode" Value="BitmapCache"/>
</Style>- Marked As Answer byJustTom Wednesday, November 04, 2009 8:35 AM
- Unmarked As Answer byJustTom Wednesday, November 04, 2009 8:49 AM
- Proposed As Answer byBrendan Clark - MSFT Wednesday, November 04, 2009 3:02 AM
- Marked As Answer byJustTom Thursday, November 05, 2009 1:18 AM
- THe problem is mostly one of me still getting my hands wet in WPF ;) I totally overlooked that - including the lttle fact that I even did have the style already defined in my XAML code ;) Nice ;)
THe final part of the question then is - how do I set parameters in there?
If I want to have
<BitmapCache EnableClearType="True" RenderAtScale="1" SnapsToDevicePixels="False" />
What would be the equivalent? I am particularly interested in EnableClearType - the text now is fast but looks blurry ;) - If you enable ClearType you'll also want to set SnapsToDevicePixels to true. Snapping the cache to pixels should make everything less blurry, including the text. The example XAML I gave you cheated a bit since we added a TypeConverter that creates a default-valued BitmapCache if you use the shorthand notation I used. The verbose way that allows you to specify all the properties would look like this:
<Style TargetType="ListBoxItem" x:Key="ContainerStyle">
<Setter Property="CacheMode">
<Setter.Value>
<BitmapCache EnableClearType="True" SnapsToDevicePixels="True"/>
</Setter.Value>
</Setter>
</Style>- Proposed As Answer byBrendan Clark - MSFT Wednesday, November 04, 2009 8:04 PM
- Marked As Answer byJustTom Thursday, November 05, 2009 1:11 AM
- Thanks ;) Will try ;) I still am stumbling like mad with the WPF delicacies ;)
Funny how performance went up with caching (and text quality down) ;) Still not good enough, though ;) Will have to open another post with some concrete scenario.
THanks for the help here ;) This Bitmap Caching is a great addition ;)


