Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 可拖动silverlight用户控件_C#_Xaml_Silverlight - Fatal编程技术网

C# 可拖动silverlight用户控件

C# 可拖动silverlight用户控件,c#,xaml,silverlight,C#,Xaml,Silverlight,我在silverlight中创建了一个draggaable用户控件,但在页面内移动控件时遇到问题。事实上,问题是,当拖动控件时,它会移出页面,这是我不想要的。我希望控件只能拖动到父控件内部 如有任何帮助/建议,将不胜感激 使用TranslateTransform时,将向合成器线程发送指令,以在X/Y偏移处显示元素。它不遵守面板和任何可视树成员的规则。因此,您必须告诉合成器线程有关边界的信息。像这样使用剪辑: <Grid Width="500" Height="500"> <

我在silverlight中创建了一个draggaable用户控件,但在页面内移动控件时遇到问题。事实上,问题是,当拖动控件时,它会移出页面,这是我不想要的。我希望控件只能拖动到父控件内部

如有任何帮助/建议,将不胜感激

使用TranslateTransform时,将向合成器线程发送指令,以在X/Y偏移处显示元素。它不遵守面板和任何可视树成员的规则。因此,您必须告诉合成器线程有关边界的信息。像这样使用剪辑:

<Grid Width="500" Height="500">
  <Grid.Clip>
       <RectangleGeometry Rect="0,0,500,500" /> 
   </Grid.Clip>

   <Grid x:Name="LayoutRoot">
        <!-- This is the element being dragged by the mouse -->
   </Grid>
</Grid>

对不起,我没把它弄清楚。在何处添加网格。剪辑?在父网格中?是的,父网格或任何面板。请看我的更改。非常感谢您的回复。但我想我错过了什么。请查看附件中的详细信息。如果您想让生活更轻松,只需将silverlight MouseDragElementBehavior放到对象上,并设置ConstrainToParentBounds=True,瞧,完成。。。
<Grid Width="500" Height="500">
  <Grid.Clip>
       <RectangleGeometry Rect="0,0,500,500" /> 
   </Grid.Clip>

   <Grid x:Name="LayoutRoot">
        <!-- This is the element being dragged by the mouse -->
   </Grid>
</Grid>
private void moveUserControl (MouseEventArgs e)
{
    Point mousePos = e.GetPosition(LayoutRoot);
    Double newX = LocalTranslateTransform.X + (mousePos.X - DragOffset.X);
    Double newY = LocalTranslateTransform.Y + (mousePos.Y - DragOffset.Y);

    var minX = 0;
    var maxX = 500 - ActualWidth; // 500 is parent width
    var minY = 0;
    var maxY = 500 - ActualHeight; // 500 is parent height

    if (newX < minX)
    {
        newX = minX;
    }
    else if (newX > maxX)
    {
        newX = maxX;
    } 

    if (newY < minY)
    {
        newY = minY;
    }
    else if (newY > maxY)
    {
        newY = maxY;
    } 

    LocalTranslateTransform.X = newX;
    LocalTranslateTransform.Y = newY;
}