enable child control while Custom parent is disabled in wpf C#

爷,独闯天下 提交于 2021-02-04 08:36:05

问题


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>

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>

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);
    }

Here Is Image Measurement

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

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
        }
    }

And Here is Property For Enabling

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

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

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

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

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


回答1:


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

From the docs:

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

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



来源:https://stackoverflow.com/questions/64029418/enable-child-control-while-custom-parent-is-disabled-in-wpf-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!