【问题标题】:Adding controls on mousedown and having new control get the mousedown在 mousedown 上添加控件并让新控件获取 mousedown
【发布时间】:2017-02-22 16:17:26
【问题描述】:

我有使用 Thumb 可以拖动的矩形。可以通过单击并拖动矩形上的任意位置来拖动它们。

现在,我希望能够在用户鼠标在父画布上向下居中的位置添加一个矩形。当我这样做时,我希望矩形接收鼠标按下并能够被拖动。

但是,在我再次上下鼠标之前,矩形不会收到任何信息。

我尝试了 previewMouseDown 等,我尝试了 e.Handled = true 等,但它们都不起作用。该事件始终由父画布使用。

在预览之上是否有一种机制可以像: 好吧,现在你设置为单击父画布,然后我添加矩形,哦,但是现在你设置为单击矩形,所以最终我将鼠标按下到矩形。

【问题讨论】:

    标签: c# wpf mouseevent


    【解决方案1】:

    我还没有看到你的代码,因此我不能肯定地说..假设你使用 mvvmlight 的交互事件触发来进行 mousedown.. 你可以做的是在矩形类中实现你的 mousedown 命令,然后你应该能够拖动你的矩形...... 假设您的 c# 中有一个画布类 (canvas.cs),在您的 canvas.cs 中有一个对象调用 Rect (Rectangle Rect = new Rectangle(...)),您可以在矩形内实现 mousedown 命令。 cs 而不是 canvas.cs。有很多方法可以做到这一点..或在主窗口的 xaml 中..您可以执行引用 Rect 类的绑定..例如,在您的 mainwindow.xaml.cs 中,您将 datacontext 设置为 Canvas.cs .. 在您的 mainwindow.xaml 中,您可以像这样绑定 mousedown:

    <Window 
                xmlns:i="clr namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
                xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WPF45">
        <i:Interaction.Triggers>
              <i:EventTrigger EventName="MouseDown or PreviewMouseDown">
                   <cmd:EventToCommand Command="{Binding Canvas.Rect.MouseDownCommand}"/>
              </i:EventTrigger>
        </i:Interaction.Triggers>
    </Window>
    

    或者,你也可以这样做

    <Your Control Here>
       <Border>
           <Border.InputBinding>
              <MouseBinding MouseAction="LeftClick" Command="{Binding DataContext.SomeCommand}"/>
            </Border.InputBindings>
      </Border>
    </Your Control Here>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多