![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 |
![]() |
| 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 |