mikkelbm

Hi

I'm trying to create an album-viewer of all my music albums. To this I'm using a ListBox with Orientation="Horizontal" - this is all fine, and I can see my albums. But my issue is, that I want to center the item I just pressed. How can I do that Is there an alternative to ListBox which is better to use for this purpose



Re: Windows Presentation Foundation (WPF) Listbox - Center item

Josh Smith

I'm not clear on what you're trying to do. When you say that the ListBox's Orientation="Horizontal" do you mean that its ItemsPanel's Orientation is Horizontal What do you mean by saying that the selected item should be "centered" Centered vertically Horizontally




Re: Windows Presentation Foundation (WPF) Listbox - Center item

mikkelbm

1) Yes, the ItemsPanel is horizontal.
2) I want the selected item to be centered in the visible part of the listbox.

For example:

There are 50 elements in the listbox.
The listbox can show 9 (index 0-8) elements at a time. (what I call the "visible part")
Element with index 4 is now the centered element.
If I press element number 8 the items should scroll so index 8 now is in the center of the listbox.

/Mikkel






Re: Windows Presentation Foundation (WPF) Listbox - Center item

mikkelbm

What I'm trying to create is a simple version of iTunes albumviewer.
In iTuns it's a 3D albumviewer, but I only need a 2D version of that - optimized for a touch-screen. My skills with .NET 3.0 are not yet good enough to create 3D applications :)





Re: Windows Presentation Foundation (WPF) Listbox - Center item

iamaprogrammer

I had to solve this problem recently... Actually, I believe my requirements were even more difficult - as my application has all the restrictions of a 10ft application (i.e., no mouse - only keyboard navigation) - and the selected item always has to be in the center of the list (unless the list has less than the number of visible items, in which case the selection can move)... so basically I had to turn off all of the standard ScollViewer bits.

I wasn't familiar enough with WPF to solve this problem elegantly (well, now that I think about it, my solution is fairly simple) - but does require a bit of code. Basically what I ended up doing was creating a UserControl and wrapping a ListBox control inside a Canvas inside a Border. So my heirarchy looks like UserControl->Border->Canvas->ListBox. The Border has its ClipToBounds set to true, and so I basically scroll the ListBox using Canvas coords and use ListBoxItem width and height attributes to measure the scroll amounts. The UserControl has a dependency property which is the desired number of visible items in the list - and uses this number to dynamically size the Canvas (and its parent, the border) so that only those number of items are visible.

One of the drawbacks with this implementation is that your UserControl needs to know about changes to the data in the ListBox - in case it needs to re-center the list - and this is what I am currently stuck on - and have asked for help with in this post. I can't figure out how to get callbacks in my UserControl whenever the item collection of the ListBox changes.... I have added a SourceUpdated event to my ListBox, but its not getting called. Uhg... there is sooo much to learn about this framework.





Re: Windows Presentation Foundation (WPF) Listbox - Center item

mikkelbm

>> Iamaprogrammer

Can you post some essential pieces of your code to give me an idea on how it should be done





Re: Windows Presentation Foundation (WPF) Listbox - Center item

Matt Hohn - MSFT

You should be able to take control of the ScrollViewer in the ListBox and programmatically scroll that to center your item.   You can set the vertical offset of the ScrollViewer to the item or pixel value.






Re: Windows Presentation Foundation (WPF) Listbox - Center item

mikkelbm

How can i retrieve the ScrollViewer I can't find it as a property, and the only method I can find is ScrollIntoView (object obj), which I can't use for this purpose.





Re: Windows Presentation Foundation (WPF) Listbox - Center item

Matt Hohn - MSFT

You will have to dig into the Visual Tree. This post may help you, if not let me know and I can post some more code samples¡­

http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=989561&SiteID=1






Re: Windows Presentation Foundation (WPF) Listbox - Center item

mikkelbm

I've now read the thread, but I'm not sure I fully understand the VisualTreeHelper.GetChild - if I type the following:

ScrollViewer sv = VisualTreeHelper.GetChild(listBoxCovers, 0) as ScrollViewer;

sv will be null. (how can I know the index )

My xaml code looks like this:

<ScrollViewer>
<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" MouseDoubleClick="ListMouseUp" Background="Transparent" Margin="11,13,11,0" Name="listBoxCovers" Height="204" VerticalAlignment="Top">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
</ScrollViewer>

Maybe you can post some of the code samples you were talking about


/Thanks





Re: Windows Presentation Foundation (WPF) Listbox - Center item

Josh Smith

In the post that Matt linked you to, you need to use the FindScroll method to get the ScrollViewer in the ListBox. By default the ListBox's first visual child is a Border. It's child is a ScrollViewer.




Re: Windows Presentation Foundation (WPF) Listbox - Center item

mikkelbm

Ahh... Yes, off course. I don't know why I missed that part. Thanks.

I'll have a look at it and returns later.





Re: Windows Presentation Foundation (WPF) Listbox - Center item

Boman

Hii Mikkel

Did you get the centering working .. I¡¯am having the same problem .. I would like it it to animate to its center possition

/Bo





Re: Windows Presentation Foundation (WPF) Listbox - Center item

mikkelbm

Unfortunately not.

But if you find a solution for this, please post it here Smile






Re: Windows Presentation Foundation (WPF) Listbox - Center item

Matt Hohn - MSFT

What were you not able to get working Give me a code sample or explain what you are trying to do again and i can take a look.