Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > Avalon

How to navigate through ListBox items via a style/template?

Closed Thread
 
Thread Tools Display Modes
Old 01-31-2006   #1 (permalink)
Pascal Bourque
Guest


 

How to navigate through ListBox items via a style/template?

Hi,

I'm using a styled ListBox to present a list of records from a database.
I want the ListBox to display only one item at a time and provide 2
buttons for navigating through the items. Here's the [ugly but working]
style I have so far:

<Style x:Key="CategoryListStyle"
TargetType="{x:Type ListBox}"
>

<Setter Property="SelectionMode"
Value="Single" />

<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBox}">
<DockPanel>
<StackPanel DockPanel.Dock="Bottom"
Orientation="Horizontal"
HorizontalAlignment="Center"
>

<Button>Previous</Button>
<TextBlock Text="Category " />
<TextBlock Text="{Binding
Path=SelectedIndex,RelativeSource=/TemplatedParent}" />
<TextBlock Text=" of " />
<TextBlock Text="{Binding
Path=Items.Count,RelativeSource=/TemplatedParent}" />
<Button>Next</Button>
</StackPanel>

<ContentPresenter Content="{TemplateBinding
SelectedItem}"
ContentTemplate="{TemplateBinding
ItemTemplate}" />
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


It works as expected, but I can't figure out how to hook my "Previous"
and "Next" buttons to the actions they are meant to perform (i.e.
increment/decrement the value of ListBox.SelectedIndex for the templated
ListBox).

I tried binding the buttons to commands from ComponentCommands
(MoveLeft,MoveDown, etc) but it did not work.

I also tried using EventTriggers within the ControlTemplate declaration
to animate the SelectedIndex property, but I could not find a way to
reference the "/TemplatedParent" in StoryBoard.TargetName property:

(for this, I added x:Name attributes to the button declarations in the
template above)

<ControlTemplate.Triggers>
<EventTrigger SourceName="PreviousButton"
RoutedEvent="Button.Click"
>

<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<Int32Animation By="-1"
Duration="0:0:0"
FillBehavior ="HoldEnd"
Storyboard.TargetName="/TemplatedParent"

Storyboard.TargetProperty="SelectedIndex" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>

<EventTrigger SourceName="NextButton"
RoutedEvent="Button.Click"
>

<EventTrigger.Actions>
<BeginStoryboard>
<Storyboard>
<Int32Animation By="1"
Duration="0:0:0"
FillBehavior ="HoldEnd"
Storyboard.TargetName="/TemplatedParent"

Storyboard.TargetProperty="SelectedIndex" />
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
</ControlTemplate.Triggers>


Looking at the ListBox code via Reflector seem to indicate that item
navigation is hardwired in ListBox.OnKeyDown, so it seems that I would
have no choice but to derive from ListBox to achieve what I want...

Thoughts, suggestions?

Thanks!

Pascal
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Vista Won't Let Me Create XP-Style Directory Tree for Old Template Files Michael Wyland Vista file management 6 06-12-2008 03:18 PM
ListBox, Style & ContextMenu Pan Kessel Avalon 0 10-11-2007 04:16 AM
Setting up collections in a style/template David Cater Avalon 2 06-11-2007 03:49 PM
Textbox value to filter databound ListBox items? Olav Avalon 2 09-23-2006 12:55 PM
How to sort items of a ListBox bound to an XmlDataProvider? Josh Smith Avalon 4 06-11-2006 01:02 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50