wpf - How to get the X,Y position of a UserControl within a Canvas -
i have simple usercontrol below - place in canvas. move using multi-touch , want able read new x,y postion using procedural c# code. ideally have x , y 2 properties or 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 create attached property each of 'x' , 'y' , fill them canvas.left , canvas.top, using binding or form of attached property or ???. have spent quite time searching solution, find seems 'not quite needed'.
you can use 2 properties in usercontrol,
public double top { { if (double.isnan(canvas.gettop(this))) { return (double)0; } return canvas.gettop(this); } set { canvas.settop(this, value); } }
the check "notanumber" not realy necessary, use prevent errors.
Comments
Post a Comment