代码之家  ›  专栏  ›  技术社区  ›  Brian Genisio

多点触控滚动浏览吃触摸事件

  •  3
  • Brian Genisio  · 技术社区  · 14 年前

    WPF 4:

    我有一个滚动浏览器,里面有很多滑块。我希望滚动浏览与触摸,我希望内部滑块也回应触摸。

    不幸的是,滚动查看器正在吃掉“touchMove”事件,而不是将它们传递给滑块控件。知道怎么解决吗?

    这是我的xaml:

    <Window x:Class="ScrollingTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.Template>
                    <ControlTemplate>
                        <ScrollViewer VerticalScrollBarVisibility="Auto" PanningMode="Both" >
                            <ItemsPresenter />
                        </ScrollViewer>
                    </ControlTemplate>
                </ItemsControl.Template>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border Height="100" BorderThickness="2" BorderBrush="Black">
                            <Slider Value="{Binding ., Mode=TwoWay}" Width="300" Minimum="0" Maximum="100" />
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </Window>
    

    我的密码是:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = Items;
        }
    
        public IEnumerable<int> Items
        {
            get
            {
                return Enumerable.Range(0, 50);
            }
        }
    }
    
    4 回复  |  直到 14 年前
        1
  •  2
  •   Community T.Woody    7 年前

    请看我对这个问题的回答: ScrollViewer in a touch interface not working properly

    我刚刚在我们的应用程序中解决了这个问题,并且使用了一个自定义的拇指控件-在我的回答中,我解释了导致这个问题的原因。

        2
  •  0
  •   LBugnion    14 年前

    这听起来像是“路由事件标记为已处理”的情况。您能否尝试使用addhandler订阅该事件,并将最后一个参数“handledeventstoo”设置为true?

    干杯, 劳伦特

        3
  •  0
  •   Rob Perkins    14 年前

    很可能是在处理触动事件。您可以在滑块控件中处理冒泡事件(previewtouchmove等)。您需要协调您希望如何处理触摸事件。

        4
  •  0
  •   Roman Barinov    14 年前

    您可以尝试使您的自定义类派生自scrollviewer并重写ontouchmove方法。

    public class CustomScrollViewer : System.Windows.Controls.ScrollViewer
    {
        protected override void OnTouchMove(System.Windows.Input.TouchEventArgs e)
        {
            // delete the base.OnTouchMove() call to prevent event being "eat" :)
        }
    }
    

    然后,像这样编辑xaml:

    <Window x:Class="ScrollingTest.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <ItemsControl ItemsSource="{Binding}">
                <ItemsControl.Template>
                    <ControlTemplate>
                        <local:CustomScrollViewer VerticalScrollBarVisibility="Auto" PanningMode="Both" >
                            <ItemsPresenter />
                        </local:CustomScrollViewer>
                    </ControlTemplate>
                </ItemsControl.Template>
                <ItemsControl.ItemTemplate>
                    <DataTemplate>
                        <Border Height="100" BorderThickness="2" BorderBrush="Black">
                            <Slider Value="{Binding ., Mode=TwoWay}" Width="300" Minimum="0" Maximum="100" />
                        </Border>
                    </DataTemplate>
                </ItemsControl.ItemTemplate>
            </ItemsControl>
        </Grid>
    </Window>