gpt4 book ai didi

WPF ScrollViewer : tap=click, 点击并按住/拖动=滚动。如何做到这一点?

转载 作者:行者123 更新时间:2023-12-04 15:55:04 27 4
gpt4 key购买 nike

我正在开发一个 WPF 触摸应用程序。我有一个包含按钮的滚动查看器。我想在触摸拖动按钮时滚动显示,并在点击时调用按钮的命令。以下是一些入门代码:

<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>

和 View 模型:
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()); }
}
}
}

预期行为:
  • 当用户点击一个按钮时,会出现一个消息框,其中包含“Button clicked!”文本。 ==> 好的
  • 当用户按下按钮并移动手指(不松开)时,按钮会上下滚动 ==> NOK

  • 如何在包含按钮的 ScrollViewer 中实现滚动?

    我正在 Windows 7 上的 Visual Studio 2013 上进行开发,并且我的目标是具有相同代码库的 Windows 7 桌面和 Windows 8 平板电脑。框架 4.0。如果真的有必要,我可以升级到4.5.2(我们有很多用户,所以升级不是小事)。

    最佳答案

    由于有 Button(s)ListView我们必须为此制作 command按预期执行。这就是棘手的部分。我是通过以下方式做到的:

    XAML:

    <Window.DataContext>
    <local:MyViewModel />
    </Window.DataContext>

    <Grid>
    <ScrollViewer>
    <ListView ItemsSource="{Binding MyData}" HorizontalAlignment="Stretch" Name="listview" ScrollViewer.PanningMode="VerticalOnly">
    <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.Resources>
    <Style TargetType="Button">
    <EventSetter Event="PreviewMouseMove" Handler="PreviewMouseMove" />
    <EventSetter Event="Drop" Handler="Drop" />
    <Setter Property="AllowDrop" Value="True" />
    </Style>
    </ListView.Resources>
    </ListView>
    </ScrollViewer>
    </Grid>

    查看型号:
    class MyViewModel
    {
    public MyViewModel()
    {
    MyCommand = new ICommandImplementation();
    }

    public ObservableCollection<string> MyData
    {
    get
    {
    return new ObservableCollection<string>(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()); }
    }
    }

    事件:
     private void Drop(object sender, DragEventArgs e)
    {
    var source = e.Data.GetData("Source") as string;
    if (source != null)
    {
    int newIndex = listview.Items.IndexOf((sender as Button).Content);
    var list = listview.ItemsSource as ObservableCollection<string>;
    list.RemoveAt(list.IndexOf(source));
    list.Insert(newIndex, source);
    }
    }

    private void PreviewMouseMove(object sender, MouseEventArgs e)
    {
    if (e.LeftButton == MouseButtonState.Pressed)
    {
    Task.Factory.StartNew(new Action(() =>
    {
    Thread.Sleep(500);
    App.Current.Dispatcher.BeginInvoke(new Action(() =>
    {
    if (e.LeftButton == MouseButtonState.Pressed)
    {
    var data = new DataObject();
    data.SetData("Source", (sender as Button).Content);
    DragDrop.DoDragDrop(sender as DependencyObject, data, DragDropEffects.Move);
    e.Handled = true;
    }
    }), null);
    }), CancellationToken.None);
    }
    }

    In this link我已经写了 Drag & Drop关于事件及其数据共享的工作。

    而且我仍在研究滚动部分。

    关于WPF ScrollViewer : tap=click, 点击并按住/拖动=滚动。如何做到这一点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37008970/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com