Question

I got a working code for scaling frameworkelements in WPF via the ManipulationDelta method. It works with all kind of elements, as buttons, shapes or textblock, but not with textboxes.

Does anybody know how I can fix it?

Edit: Here´s a simpified Version of my Code:

 private void canvas_ManipulationDelta(object sender, ManipulationDeltaEventArgs  e)
    {
        var element = e.Source as FrameworkElement;
        var transformation = element.RenderTransform as MatrixTransform;
        var matrix = transformation == null ? Matrix.Identity : transformation.Matrix;

            matrix.ScaleAt(e.DeltaManipulation.Scale.X,
           e.DeltaManipulation.Scale.Y,
            1, 
           1);
        }

        matrix.RotateAt(e.DeltaManipulation.Rotation,
                        e.ManipulationOrigin.X,
                        e.ManipulationOrigin.Y);

        matrix.Translate(e.DeltaManipulation.Translation.X,
                         e.DeltaManipulation.Translation.Y);

        element.RenderTransform = new MatrixTransform(matrix);

        e.Handled = true;

       }

The elements are created genericly, but it´s the same as this xaml:

        <Canvas Name="SomeCanvas" ManipulationDelta="canvas_ManipulationDelta">
            <TextBox   Canvas.Left="400" Canvas.Top="200" Height="50" Name="s3" IsManipulationEnabled = "true" Background="#57FF3ACB"  />
        </Canvas>
Was it helpful?

Solution

This is because TextBox handles MouseDown events internally. They never get called, so the manipulation can't work. Here is the explanation:

http://msdn.microsoft.com/es-es/library/ms750580(v=vs.85).aspx

Alternatively, you could wrap the TextBox in a Border:

<Canvas Name="SomeCanvas" ManipulationDelta="canvas_ManipulationDelta">
    <Border Canvas.Left="400" Canvas.Top="200" Background="Transparent" IsManipulationEnabled="True">
        <TextBox Height="50" Name="s3" Background="#57FF3ACB" IsHitTestVisible="False" />
    </Border>
</Canvas>

Put the IsHitTestVisible property to False in order to let the Mouse events pass from the TextBox to the Border.

Furthermore, you need to set the Border's Background to make it hit test visible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top