3

I have a simple UserControl implemented as below - which I place in a Canvas. I move it using Multi-touch and I want to be able to read its new X,Y postion using procedural C# code. Ideally I would like to have X and Y as two properties or as a Point (X,Y).

<UserControl x:Class="TouchControlLibrary.myControl"
             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="64" d:DesignWidth="104">
    <Border Name="ControlBorder" BorderThickness="1" BorderBrush="Black">
        <DockPanel Margin="1" Height="60 " Width="100">
            <StackPanel  DockPanel.Dock="Left" Background="Gray"  Width="20" >
                <Button Background="#FFDEDE53" Padding="0">In</Button>
            </StackPanel>
            <StackPanel  DockPanel.Dock="Right" Background="Gray"  Width="20" >
                <Button Background="#FFE8B48F" Padding="0">Out</Button>
            </StackPanel>
        </DockPanel>  
    </Border>
</UserControl>

I expected to be able to create an attached property for each of 'X' and 'Y' and fill them from Canvas.Left and Canvas.Top, using binding or some form of attached property or maybe something else entirely.

However, despite spending quite some time searching for a solution, everything I found so far seems to be 'not quite what is needed'.

What would you suggest I do to solve this problem?

Will Vousden
  • 32,488
  • 9
  • 84
  • 95
AlanW
  • 53
  • 1
  • 5

2 Answers2

1

let me see if I have this right, you create a new instance of the user control and add it to a canvas? Then you move it using Multi-touch (how do you do this? via a behaviour or manually?), and at any one time you want to read the new X/Y position of the instance of the user control relative to the Canvas?

Why cant you just read the same thing that is being changed when you move it via touch? Have you had a look at the RenderTransform property, this is what is usually changed when you manipulate something using a behavior or manual adjustments.

Mark
  • 14,820
  • 17
  • 99
  • 159
  • 2
    Your descrition is correct - and I move it manually using touch. I have been able to get the position via: Mp = element.TranslatePoint(new Point(0, 0), null); where element is my control and Mp is a Point structure. I then use Mp.X and Mp.Y However I had hoped to encapsulate the X,Y as properties of my control and simplify access. Perhaps I am trying to do the wrong thing. BTW, this is/was my first post and I managed to end up submitting the same question twice! Sorry about that. – AlanW Feb 18 '11 at 23:38
1

Well, here's a sample code i've for to get the Y, you can figure out the X hope this is what you need:

Point current = e.GetPosition(MyControl as UIElement); //where your control is
Point top = e.GetPosition(Canvas as UIElement); //or maybe just the height of the canvas
Thickness margin = new Thickness();
margin.Top = top.Y - current.Y; //distance from the top
MyControl.Margin = margin;
Notter
  • 588
  • 4
  • 16
  • many thanks, I will try this - as per my other comment I was trying to encapsuate this in the control itself. That may be the wrong approach! – AlanW Feb 18 '11 at 23:40