I'm developping a WPF touch application. I have a scrollviewer containing buttons. I want to scroll the display when I touch-drag the buttons, and call the button's command when I tap. Here's some code to get started:
<Window x:Class="wpf_Button_Scroll.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:wpf_Button_Scroll"
Title="MainWindow" Height="350" Width="200">
<Window.DataContext>
<my:MyViewModel />
</Window.DataContext>
<Grid>
<ScrollViewer>
<ListView ItemsSource="{Binding MyData}" HorizontalAlignment="Stretch">
<ListView.ItemTemplate>
<DataTemplate>
<Button Content="{Binding}"
Command="{Binding DataContext.MyCommand, RelativeSource={RelativeSource AncestorType=ItemsControl}}"
CommandParameter="{Binding}"
Margin="5 2" Width="150" Height="50"
FontSize="30" />
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollViewer>
</Grid>
</Window>
And the view model:
namespace wpf_Button_Scroll
{
class MyViewModel
{
public MyViewModel()
{
MyCommand = new ICommandImplementation();
}
public string[] MyData
{
get
{
return new string[]{
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty"
};
}
}
public ICommand MyCommand { get; private set; }
private class ICommandImplementation : ICommand
{
public bool CanExecute(object parameter) { return true; }
public event EventHandler CanExecuteChanged;
public void Execute(object parameter) { System.Windows.MessageBox.Show("Button clicked! " + (parameter ?? "").ToString()); }
}
}
}
Intended behavior:
when the user taps a button, a message box appears with the text "Button clicked!" ==> OK
when the user presses a button and moves his finger (without releasing), the buttons scroll up and down ==> NOK
How can I achieve scrolling in a ScrollViewer that contains buttons?
I'm developping on Visual Studio 2013 on Windows 7, and I'm targeting a Windows 7 desktop and a Windows 8 tablet with the same codebase. Framework 4.0. If it's really necessary, I can upgrade to 4.5.2 (we have many users, so it's not trivial to upgrade).