在WPF C#中禁用自定义父级时启用子控件 [英] enable child control while Custom parent is disabled in wpf C#

查看:69
本文介绍了在WPF C#中禁用自定义父级时启用子控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我为缩放Picture定制了父控件.在该控件内有子画布,在画布内有子图像控件.我创建了一个用于缩放和图像测量的菜单.我还可以测量图像的坐标.我禁用父级ZoomBorder控件时遇到的问题是我无法触发Child Image事件.我可以禁用名为ZoomBorder的ZoomControl,但是我想要MouseDown的图像事件.当启用Zoomcontrol并可以正常工作时,我可以禁用子图像事件.这是我的代码.

Hello I made custom made parent control for zooming Picture . Inside That Control there is child canvas and inside Canvas There is child image Control . And I created A menu for Zoom and Image measuring . I also can able to measure co Ordinates the image . My problem when I disabled Parent ZoomBorder control I cannot triggering event of Child Image . I can disable ZoomControl named ZoomBorder but I want image event of MouseDown . I can Disable child Image event when Zoomcontrol enabled and It works. Here is my code .

<utils:ZoomBorder x:Name="ZoomBorder"   IsEnabled="{Binding IsEnableZoom}"   ClipToBounds="True" Background="Gray" Grid.Row="3" Grid.Column="2" Grid.ColumnSpan="4">
        <Canvas IsEnabled="{Binding IsEnableCanvas}">
            <Image  x:Name="DIIMGFINAL" cal:Message.Attach="[Event MouseDown] = [Action MDownCalCulateDistance($source, $eventArgs)];
                [Event MouseUp] = [Action MUpCalCulateDistance($source, $eventArgs)];
                [Event MouseMove] = [Action MMoveCalCulateDistance($source, $eventArgs)]"/>
            <Line IsHitTestVisible="False"  X1="{Binding FirstPoint.X}" Y1="{Binding FirstPoint.Y}"
                  X2="{Binding SecondPoint.X}" Y2="{Binding SecondPoint.Y}"
                  Stroke="Red" StrokeThickness="3"/>
        </Canvas>
    </utils:ZoomBorder>

这是菜单项ImageMeasuring和Zoom

And Here is The Menu Item ImageMeasuring and Zoom

<Menu Grid.ColumnSpan="3" Grid.Column="1" Grid.Row="0"  Background="Transparent" >
        <MenuItem x:Name="ズームControl" Header="ズーム">
            <!--Item For Zoom-->
        </MenuItem>

        <MenuItem x:Name="Img_Measurement" Header="Image Measurement">
            <!--Item For Measure-->
        </MenuItem>
    </Menu>

这是我的C#代码以进行缩放激活

Here is My C# code For Zoom Activation

  public void ズームControl()
    {
        //For Only Zoom
        IsEnableZoom = true;
        IsEnableCanvas = false;
        MessageBox.Show("Zoom Control Starts", "Alert", MessageBoxButton.OK, MessageBoxImage.Exclamation);
    }

这里是图像测量

 public void Img_Measurement()
    {
        IsEnableZoom = false;
        IsEnableCanvas = true;
        MessageBox.Show("Image Measurement Starts", "Alert", MessageBoxButton.OK, MessageBoxImage.Exclamation);
    }

这是我的图像MouseDown事件

Here Is My Image MouseDown Event

public void MDownCalCulateDistance(object sender, System.Windows.Input.MouseEventArgs e)
    {
         f (IsEnableCanvas == false) return;

        if (!(sender is System.Windows.Controls.Image)) return;
        try{
            //Code Here
        }
        Catch(Exception ex){
            //code here
        }
    }

这是要启用的属性

 public bool IsEnableZoom
    {
        get
        {
            return _isEnableZoom;
        }
        set
        {

            _isEnableZoom = value;
            NotifyOfPropertyChange(() => IsEnableZoom);
        }
    }

    public bool IsEnableCanvas
    {
        get
        {
            return _isEnableCanvas;
        }
        set
        {

            _isEnableCanvas = value;
            NotifyOfPropertyChange(() => IsEnableCanvas);
        }
    }

如您所见,当我关闭ZoomControl的ZoomBorder时,我无法进行MouseDown事件.当启用Parent事件的ZoomBorder时,我可以使Mousedown消失.请帮我 .预先感谢

As You can See When I disbled the ZoomBorder of ZoomControl I cannot go The MouseDown Event . I can Mousedown disbled when ZoomBorder of Parent event enabled . Please Help me . Thanks in advance

推荐答案

禁用的元素不会引发鼠标事件.这是设计使然.

Disabled elements don't raise mouse events. This is by design.

未启用的元素不会参与命中测试或焦点,因此不会成为输入事件的来源.

Elements that are not enabled do not participate in hit testing or focus and therefore will not be sources of input events.

显而易见的解决方案是不禁用 Canvas .您仍然可以使其外观和/或表现为已禁用.

The obvious solution is not to disable the Canvas. You may still make it look and/or behave like it was disabled.

这篇关于在WPF C#中禁用自定义父级时启用子控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆