有没有一种简单的方法可以使用此拇指控件挂钩DragOver事件?

时间:2015-10-01 19:18:21

标签: c# wpf events slider

在这段代码中,我有2个椭圆,每个都包含在自己的拇指控件中。在代码隐藏中,Thumb_DragDelta允许我自由移动对象。我的问题是: -

  1. 当我将一个事件拖到另一个事件上时,如何挂钩事件(例如 Ellipse1拖过Ellipse2)。我已经尝试过设置代码了 DragOver和DragEnter但没有任何反应。
  2. 在上面1.的事件处理程序中,我如何访问属性 例如,Ellipse2改变填充颜色?
  3. 这是xaml: -

    <Window x:Class="DragDropExample.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:DragDropExample"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <Canvas>
                <Thumb x:Name="Thumb1" Canvas.Left="100" 
                       Canvas.Top="60" 
                       DragDelta="Thumb_DragDelta" 
                       Panel.ZIndex="1">
                    <Thumb.Template>
                        <ControlTemplate>
                            <Ellipse x:Name="Ellipse1" 
                                     Fill="#FF6F1D9E" 
                                     Height="85" 
                                     Canvas.Left="379" 
                                     Stroke="Black" 
                                     Canvas.Top="20" 
                                     Width="84" 
                                     HorizontalAlignment="Right" 
                                     Margin="0,40,36.5,0" 
                                     VerticalAlignment="Top" />
                        </ControlTemplate>
                    </Thumb.Template>
                </Thumb>
                <Thumb x:Name="Thumb2" Canvas.Left="400" 
                       Canvas.Top="60" 
                       DragDelta="Thumb_DragDelta" 
                       Panel.ZIndex="0" 
                       AllowDrop="True">
                    <Thumb.Template>
                        <ControlTemplate>
                            <Ellipse x:Name="Ellipse2" 
                                     Fill="#FF9E1D40" 
                                     Height="85" 
                                     Canvas.Left="379" 
                                     Stroke="Black" 
                                     Canvas.Top="20" 
                                     Width="84" 
                                     HorizontalAlignment="Right" 
                                     Margin="0,40,36.5,0" 
                                     VerticalAlignment="Top" />
                        </ControlTemplate>
                    </Thumb.Template>
                </Thumb>
            </Canvas>
        </Grid>
    </Window>
    

    这是代码隐藏中的C#: -

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
    
    namespace DragDropExample
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
    
    private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
            {
                UIElement thumb = e.Source as UIElement;
    
                Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
                Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
            }
        }
    }
    

0 个答案:

没有答案