You need to tell WPF where to start looking for the command handler. Without telling it, it will start looking from the Button
and not find anything that handles the LineDownCommand
. Unfortunately, setting it to the ListBox
will not suffice because the ScrollViewer
is inside the ListBox
as part of its template, so WPF still won't find it.
Setting it to one of the ListBoxItem
s is naff, but works:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ListBox x:Name="_listBox" ScrollViewer.VerticalScrollBarVisibility="Hidden">
<ListBoxItem x:Name="_listBoxItem">One</ListBoxItem>
<ListBoxItem>Two</ListBoxItem>
<ListBoxItem>Three</ListBoxItem>
<ListBoxItem>One</ListBoxItem>
<ListBoxItem>Two</ListBoxItem>
<ListBoxItem>Three</ListBoxItem>
<ListBoxItem>One</ListBoxItem>
<ListBoxItem>Two</ListBoxItem>
<ListBoxItem>Three</ListBoxItem>
<ListBoxItem>One</ListBoxItem>
<ListBoxItem>Two</ListBoxItem>
<ListBoxItem>Three</ListBoxItem>
</ListBox>
<Button Grid.Row="1" Command="ScrollBar.LineDownCommand" CommandTarget="{Binding ElementName=_listBoxItem}">Scroll Down</Button>
</Grid>
</Window>
A better way to do this would be to either re-template the ListBox
and stick the Button
inside the template, or to wire up the CommandTarget
in the code-behind.
HTH,
Kent