![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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. |
| |||||||
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Vista Won't Let Me Create XP-Style Directory Tree for Old Template Files | Vista file management | |||