c# - 如何获取 CustomControl 部分上的鼠标位置

标签 c# wpf

在制作自定义控件时,我试图获取 Canvas 上的鼠标位置, Canvas 是我的自定义控件内的容器。问题是,当我将自定义控件放入测试应用程序(只是窗口中的网格)时,我得到的始终是窗口本身上的鼠标位置,而不是自定义控件上的鼠标位置。

public class HueWheel : Control
{
    static HueWheel()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(HueWheel), new FrameworkPropertyMetadata(typeof(HueWheel)));
    }
    private bool _isPressed = false;
    //private Canvas _templateCanvas = null;
    private Canvas _PART_FirstCanvas;
    private Canvas _PART_SecondCanvas;
    private Slider _PART_Slider;

    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        _PART_FirstCanvas = (Canvas)GetTemplateChild("PART_FirstCanvas");
        _PART_SecondCanvas = (Canvas)GetTemplateChild("PART_SecondCanvas");
        _PART_Slider = (Slider)GetTemplateChild("PART_Slider");
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (_isPressed)
        {
            const double RADIUS = 150;
            Point newPos = e.GetPosition(_PART_SecondCanvas);
            double angle = MyHelper.GetAngleR(newPos, RADIUS);
            _PART_Slider.Value = (_PART_Slider.Maximum - _PART_Slider.Minimum) * angle  / (2 * Math.PI);
        }
    }

    protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
    {
        _isPressed = true;
    }

    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
    {
        _isPressed = false;
    }
}

这是 Generic.xaml 中包含的 xaml

<Style TargetType="{x:Type local:HueWheel}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:HueWheel}">
                <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <Slider x:Name="PART_Slider">
                        <Slider.Template>
                            <ControlTemplate>
                                <Canvas x:Name="PART_FirstCanvas" Width="300" Height="300">
                                        <Image Stretch="Fill" Source="Assets/HueCircle.PNG" Focusable="False" Height="300" Width="300" RenderTransformOrigin="0.5,0.5">
                                            <Image.RenderTransform>
                                                <TransformGroup>
                                                    <ScaleTransform/>
                                                    <SkewTransform/>
                                                    <RotateTransform Angle="270"/>
                                                    <TranslateTransform/>
                                                </TransformGroup>
                                            </Image.RenderTransform>
                                        </Image>
                                        <Ellipse Fill="Transparent" Width="300" Height="300" Canvas.Left="0" Canvas.Top="0"/>
                                        <Canvas x:Name="PART_SecondCanvas">
                                            <Line Stroke="Transparent" StrokeThickness="5" X1="150" Y1="150" X2="150" Y2="0"/>
                                            <Ellipse Fill="Black" Width="20" Height="20" Canvas.Left="140" Canvas.Top="30"/>
                                            <Canvas.RenderTransform>
                                                <RotateTransform CenterX="150" CenterY="150">
                                                    <RotateTransform.Angle>
                                                        <MultiBinding Converter="{StaticResource ValueAngleConverter}">
                                                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Value"/>
                                                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Minimum"/>
                                                            <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Maximum"/>
                                                        </MultiBinding>
                                                    </RotateTransform.Angle>
                                                </RotateTransform>
                                            </Canvas.RenderTransform>
                                        </Canvas>
                                    </Canvas>
                            </ControlTemplate>
                        </Slider.Template>
                    </Slider>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

此外,仅当鼠标位于 Canvas 上时才会输出鼠标位置,因此这里只是工作了一半......有点困惑。

这是我用于角度计算的两个函数:

public static double GetAngle(double value, double maximum, double minimum)
{
    double current = (value / (maximum - minimum)) * 360;
    if (current == 360)
        current = 359.999;

    return current;
}


public static double GetAngleR(Point pos, double radius)
{
    Point center = new Point(radius, radius);
    double xDiff = center.X - pos.X;
    double yDiff = center.Y - pos.Y;
    double r = Math.Sqrt(xDiff * xDiff + yDiff * yDiff);

    double angle = Math.Acos((center.Y - pos.Y) / r);

    if (pos.X < radius)
        angle = 2 * Math.PI - angle;

    if (Double.IsNaN(angle))
        return 0.0;
    else
        return angle;
}

有什么提示吗?

谢谢

最佳答案

_PART_SecondCanvas_PART_FirstCanvas 为 null,因为它们属于子 Slider 的控件模板,而不是“主” HueWheel 模板。因此,您无法使用 HueWheel 中的 GetTemplateChild() 访问它们。

OnApplyTemplate()中,您需要首先找到 slider ,然后应用其模板,最后调用FindName()查找 Canvas 元素的模板:

public override void OnApplyTemplate()
{
    base.OnApplyTemplate();
    //_PART_FirstCanvas = (Canvas)GetTemplateChild("PART_FirstCanvas");
    //_PART_SecondCanvas = (Canvas)GetTemplateChild("PART_SecondCanvas");
    _PART_Slider = (Slider)GetTemplateChild("PART_Slider");

    _PART_Slider.ApplyTemplate();

    var sliderTemplate = _PART_Slider.Template;
    _PART_FirstCanvas  = (Canvas)sliderTemplate.FindName("PART_FirstCanvas",  _PART_Slider);
    _PART_SecondCanvas = (Canvas)sliderTemplate.FindName("PART_SecondCanvas", _PART_Slider);
}

来源:How do I get the Children of a ContentPresenter?

一旦您真正找到 _PART_SecondCanvas_PART_FirstCanvas,您的 OnMouseMove() 函数就应该执行您期望的操作。

关于c# - 如何获取 CustomControl 部分上的鼠标位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38783568/

相关文章:

wpf - 是否可以在后面的代码中声明命名空间

wpf - Silverlight 3.0 out of browser vs WPF/Windows App - 差异总结?

c# - 如何从文件名生成安全类名?

c# - 为什么我不能在封闭泛型的继承属性中使用 C# 语法,但 CLR 语法很好

c# - 如何推迟 WPF 文本框中的更新

c# - 应用程序崩溃,事件名称为 CLR20r3?

c# - WPF RichTextBox 第一行\n 未打印

c# - SWIG、Box2D 和 C#

c# - Dapper Orm 删除异常

c# - 如何在运行时创建表达式