首页 > 解决方案 > wpf usercontrol用拇指调整大小不触发DragDelta

问题描述

我创建了一个用户控件,我想使用拇指控件调整大小。我已将控件添加到 Canvas,当我单击拇指控件时,触发的唯一事件是 Dragend 和 DragStart(按此顺序),并且 DragDelta 甚至没有触发。我发现有人在 ItemsControl 中遇到类似问题的 Thumb,但没有触发 DragDelta 事件,但我无法询问他们是否找到了解决方案。

这是代码:

    <UserControl x:Class="test.ucChair"
                         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                         mc:Ignorable="d" 
                         d:DesignHeight="53" d:DesignWidth="93" RenderTransformOrigin="0.5,0.5" Background="Beige" 
                         >  
    <Grid Name="grd" Margin="0,0,0,0">
        <Rectangle x:Name="rectAngle"  Margin="0,0,0,0" Stroke="Black"  RenderTransformOrigin="0.5,0.5" Grid.ZIndex="1000" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" MinHeight="53"  /> <!--  Width="93" Height="53"-->
        <TextBox x:Name="txtName" Text="OPEN" Margin="0,0,0,0"  VerticalAlignment="Stretch" HorizontalAlignment="Stretch" IsReadOnly="True" IsReadOnlyCaretVisible="False" TextWrapping="Wrap" Width="93" KeyDown="txtName_KeyDown" FlowDirection="RightToLeft" BorderThickness="0" FontSize="14" Cursor="Hand" TextAlignment="Center"/> 
        <Thumb Name="thmbResize" DockPanel.Dock="Right" VerticalAlignment="Bottom" Height="15" Width="15"
                   DragDelta="OnResizeThumbDragDelta" 
                   DragStarted="OnResizeThumbDragStarted" 
                   DragCompleted="OnResizeThumbDragCompleted" HorizontalAlignment="Right">
        </Thumb>
    </Grid>
</UserControl>

    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 test
{
    /// <summary>
    /// Interaction logic for ucChairLeft.xaml
    /// </summary>
    public partial class ucChair : UserControl
    {
        public ChairType AngleType;
        //?? public int SectionID;

        public ucChair()//ChairType CType)//?, int _SectionID)
        {
            InitializeComponent();
        }


        private void OnResizeThumbDragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("DragEnd");
            Mouse.Capture(null);
        }

        private void OnResizeThumbDragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
        {
            System.Diagnostics.Debug.WriteLine("DragDelta");
        }

        private void OnResizeThumbDragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
        {
            Mouse.Capture(this);
            System.Diagnostics.Debug.WriteLine("DragStart");
        }

    }
}

<Window xmlns:test="clr-namespace:test"  x:Class="test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="300" Width="300">
    <Canvas Width="300" Height="300" Background="LightBlue">
        <test:ucChair Canvas.Left="50" Canvas.Top="100" Width="50" Height="50" Background="Yellow"/>

    </Canvas>
</Window>

非常感谢任何帮助!

标签: wpfuser-controls

解决方案


推荐阅读