c# - Check if usercontrol is within Window Bounds -


i have project requires ability drag user control (hosted within grid) around screen. works fine below code:

void myusercontrol_manipulationdelta(object sender, manipulationdeltaroutedeventargs e) {     var ct = (compositetransform) rendertransform ?? new compositetransform {centerx = 0.5, centery = 0.5};         ct.translatex += e.delta.translation.x;         ct.translatey += e.delta.translation.y; } 

the issue user control can dragged way out of screen area. prevent this, tried example shown here: http://msdn.microsoft.com/en-us/library/system.windows.uielement.manipulationdelta.aspx

unfortunately, uses containingrect.contains(shapebounds) whereas in windows 8, expected replace shapebounds(this rect) point. not sure on how work this.

so question how can ensure user control or uielement cannot dragged out of window.current.bounds area in windows 8 store app?

thanks.

edit: more details on xaml structure:

the mainpage contains grid horizontal , vertical alignment set stretch. usercontrols added grid required. each usercontrol has parent grid contains 3 different views (fullscreen, window , small). views shown based on user's choice. drag behavior must applied when window grid shown. have this

<grid> <!-- parent grid on mainpage horizontal , vertical alignment stretch-->    <grid> <!-- usercontrol's main grid (added above grid via code). grid must draggable if below grid window -->          <grid /> <!-- 1 of child grids shown based on user's choice (fullscreen, window or small view).-->    </grid> </grid> 

i dont have choice in changing layout. using above manipulationdelta event in usercontrol (which enabled/disabled based on child grid shown), able drag behavior, control can go out of window bounds. there way can add below flickbehavior in winrtxamltoolkit through code instead of xaml or enable/disable behavior based on condition?

<i:interaction.behaviors>    <views:heavyflickbehavior /> </i:interaction.behaviors> 

you can check flickbehavior in winrt xaml toolkit example of how achieve parent canvas , canvas.top/left properties instead of rendertransform, assuming canvas defines bounds.

here's abstract:

private void onassociatedobjectmanipulationstarting(object sender, manipulationstartingroutedeventargs e) {     _startposition = new point(         canvas.getleft(this.associatedobject),         canvas.gettop(this.associatedobject)); }  private void onassociatedobjectmanipulationdelta(object sender, manipulationdeltaroutedeventargs manipulationdeltaroutedeventargs) {     var dx = manipulationdeltaroutedeventargs.cumulative.translation.x;     var dy = manipulationdeltaroutedeventargs.cumulative.translation.y;      var x = _startposition.x + dx;     var y = _startposition.y + dy;      if (manipulationdeltaroutedeventargs.isinertial)     {         while (x < 0 ||                x > _canvas.actualwidth - this.associatedobject.actualwidth)         {             if (x < 0)                 x = -x;             if (x > _canvas.actualwidth - this.associatedobject.actualwidth)                 x = 2 *                     (_canvas.actualwidth - this.associatedobject.actualwidth) -                     x;         }          while (y < 0 ||                y > _canvas.actualheight - this.associatedobject.actualheight)         {             if (y < 0)                 y = -y;             if (y > _canvas.actualheight - this.associatedobject.actualheight)                 y = 2 * (_canvas.actualheight - this.associatedobject.actualheight) -                     y;         }     }     else     {         if (x < 0)             x = 0;         if (x > _canvas.actualwidth - this.associatedobject.actualwidth)             x = _canvas.actualwidth - this.associatedobject.actualwidth;         if (y < 0)             y = 0;         if (y > _canvas.actualheight - this.associatedobject.actualheight)             y = _canvas.actualheight - this.associatedobject.actualheight;     }      canvas.setleft(this.associatedobject, x);     canvas.settop(this.associatedobject, y); } 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -