Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Avalon

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

 
 
Old 01-31-2006   #1 (permalink)
Pascal Bourque


 
 

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

My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
Vista Won't Let Me Create XP-Style Directory Tree for Old Template Files Vista file management


Vista Forums 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 Ltd

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