Silverlight网格根据鼠标移动调整大小

时间:2014-12-17 12:49:58

标签: silverlight grid resize

我想用鼠标调整银色网格的大小,就像我们用鼠标调整窗口大小一样。 我实现了鼠标移动事件以执行所需的任务,但它允许从顶部,左侧和右侧调整大小。仅限网格的左上边缘,而不是其他大小。

以下是我实施的代码:

XAML代码(UI)



<Border Grid.Column="0" x:Name="LayoutRoot" BorderBrush="Transparent" Padding="0" CornerRadius="3" BorderThickness="0" >
            <Border.Background>
                <SolidColorBrush x:Name="LayoutBackground" Color="WhiteSmoke" Opacity=".5" />
            </Border.Background>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"></RowDefinition>
                    <RowDefinition Height="*"></RowDefinition>
                    <RowDefinition Height="Auto"></RowDefinition>
                </Grid.RowDefinitions>
                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Border Grid.Column="0" x:Name="Layout_TopLeftEdge" Opacity=".0" Cursor="SizeNWSE" CornerRadius="5" Height="5" Width="5" Background="LightGray" VerticalAlignment="Top" HorizontalAlignment="Left"
                MouseLeftButtonDown="LayoutGrid_MouseLeftButtonDown" MouseLeftButtonUp="LayoutGrid_MouseLeftButtonUp" MouseMove="LayoutGrid_MouseMove">
                        <TextBlock x:Name="TopLeftEdge">.</TextBlock>
                    </Border>
                    <Border Grid.Column="1" x:Name="Layout_TopEdge" Opacity=".0" Cursor="SizeNS" CornerRadius="5" Height="5" Background="LightGray"
                MouseLeftButtonDown="LayoutGrid_MouseLeftButtonDown" MouseLeftButtonUp="LayoutGrid_MouseLeftButtonUp" MouseMove="LayoutGrid_MouseMove">
                        <TextBlock x:Name="TopEdge">.</TextBlock>
                    </Border>
                    <Border Grid.Column="2" x:Name="Layout_TopRightEdge" Opacity=".0" Cursor="SizeNESW" CornerRadius="5" Height="5" Width="5" Background="LightGray" VerticalAlignment="Top" HorizontalAlignment="Right"
                MouseLeftButtonDown="LayoutGrid_MouseLeftButtonDown" MouseLeftButtonUp="LayoutGrid_MouseLeftButtonUp" MouseMove="LayoutGrid_MouseMove">
                        <TextBlock x:Name="TopRightEdge">.</TextBlock>
                    </Border>
                </Grid>
                
                <Grid Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Border Grid.Column="0" x:Name="Layout_LeftEdge" Opacity=".0" Cursor="SizeWE" CornerRadius="5" Width="5" Background="LightGray" VerticalAlignment="Stretch" HorizontalAlignment="Left"
                MouseLeftButtonDown="LayoutGrid_MouseLeftButtonDown" MouseLeftButtonUp="LayoutGrid_MouseLeftButtonUp" MouseMove="LayoutGrid_MouseMove">
                        <TextBlock x:Name="LeftEdge">.</TextBlock>
                    </Border>
                    <Border Grid.Column="1" x:Name="LayoutRootBorder" Style="{StaticResource GlassBorderStyle}" MouseLeftButtonDown="LayoutRoot_MouseLeftButtonDown" MouseLeftButtonUp="LayoutRoot_MouseLeftButtonUp" MouseMove="LayoutRoot_MouseMove">
                        <ScrollViewer HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Auto">
                            <Grid x:Name="LegendGrid" >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <!--<RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />-->
                                </Grid.RowDefinitions>
                                <!--<TextBlock Grid.Row="1" Grid.ColumnSpan="2" Text="Legend" FontStyle="Normal" FontWeight="Bold" Margin="0" TextAlignment="Left" />-->

                                <!--<tree:TreeViewIcon Grid.Column="0" Style="{StaticResource IconStyle}"
                               IconType="Circle"
                               StrokeColor="Red"
                               StrokeThicknessPercent="20"
                               FillColor="LightGreen" />
          

            <tree:TreeViewIcon Grid.Column="0" Grid.Row="1" Style="{StaticResource IconStyle}"
                               IconType="Rectangle"
                               StrokeColor="Blue"
                               StrokeThicknessPercent="20"
                               FillColor="Yellow" />
            <TextBlock Grid.Column="1" Grid.Row="1" Text="Layer Name #2" Style="{StaticResource TokenTextStyle}" />-->

                            </Grid>
                        </ScrollViewer>
                    </Border>
                    <Border Grid.Column="2" x:Name="Layout_RightEdge" Opacity=".0" Cursor="SizeWE" CornerRadius="5" Width="5" Background="LightGray" VerticalAlignment="Stretch" HorizontalAlignment="Right"
                MouseLeftButtonDown="LayoutGrid_MouseLeftButtonDown" MouseLeftButtonUp="LayoutGrid_MouseLeftButtonUp" MouseMove="LayoutGrid_MouseMove">
                        <TextBlock x:Name="RightEdge">.</TextBlock>
                    </Border>
                </Grid>
                
                <Grid Grid.Row="2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                        <ColumnDefinition Width="Auto"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Border Grid.Column="0" x:Name="Layout_BottomLeftEdge" Opacity=".0" Cursor="SizeNESW" CornerRadius="5" Height="5" Width="5" Background="LightGray" VerticalAlignment="Bottom" HorizontalAlignment="Left"
                MouseLeftButtonDown="LayoutGrid_MouseLeftButtonDown" MouseLeftButtonUp="LayoutGrid_MouseLeftButtonUp" MouseMove="LayoutGrid_MouseMove">
                        <TextBlock x:Name="BottomLeftEdge">.</TextBlock>
                    </Border>
                    <Border Grid.Column="1" x:Name="Layout_BottomEdge" Opacity=".0" Cursor="SizeNS" CornerRadius="5" Height="5" Background="LightGray"
                MouseLeftButtonDown="LayoutGrid_MouseLeftButtonDown" MouseLeftButtonUp="LayoutGrid_MouseLeftButtonUp" MouseMove="LayoutGrid_MouseMove">
                        <TextBlock x:Name="BottomEdge">.</TextBlock>
                    </Border>
                    <Border Grid.Column="2" x:Name="Layout_BottomRightEdge" Opacity=".0" Cursor="SizeNWSE" CornerRadius="5" Height="5" Width="5" Background="LightGray" VerticalAlignment="Bottom" HorizontalAlignment="Right"
                MouseLeftButtonDown="LayoutGrid_MouseLeftButtonDown" MouseLeftButtonUp="LayoutGrid_MouseLeftButtonUp" MouseMove="LayoutGrid_MouseMove">
                        <TextBlock x:Name="BottomRightEdge">.</TextBlock>
                    </Border>
                </Grid>
            </Grid>
        </Border>
&#13;
&#13;
&#13;

XAML.cs代码(代码隐藏)

&#13;
&#13;
bool IsMouseDown_LayoutGrid;
        enum cornerEdge { None = 0, TopLeft = 1, TopRight = 2, BottomRight = 3, BottomLeft = 4, Top = 5, Right = 6, Bottom = 7, Left = 8 };
        cornerEdge Edge = 0;
        Point ResizeOffset;

        private void LayoutGrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            IsMouseDown_LayoutGrid = true;
            ResizeOffset = e.GetPosition(OuterGrid);
            switch ((e.OriginalSource as TextBlock).Name)
            {
                case "TopLeftEdge":
                    Edge = cornerEdge.TopLeft;
                    Layout_TopLeftEdge.CaptureMouse();
                    break;
                case "TopEdge":
                    Edge = cornerEdge.Top;
                    Layout_TopEdge.CaptureMouse();
                    break;
                case "TopRightEdge":
                    Edge = cornerEdge.TopRight;
                    Layout_TopRightEdge.CaptureMouse();
                    break;
                case "LeftEdge":
                    Edge = cornerEdge.Left;
                    Layout_LeftEdge.CaptureMouse();
                    break;
                case "RightEdge":
                    Edge = cornerEdge.Right;
                    Layout_RightEdge.CaptureMouse();
                    break;
                case "BottomLeftEdge":
                    Edge = cornerEdge.BottomLeft;
                    Layout_BottomLeftEdge.CaptureMouse();
                    break;
                case "BottomEdge":
                    Edge = cornerEdge.Bottom;
                    Layout_BottomEdge.CaptureMouse();
                    break;
                case "BottomRightEdge":
                    Edge = cornerEdge.BottomRight;
                    Layout_BottomRightEdge.CaptureMouse();
                    break;
            }
        }

        private void LayoutGrid_MouseMove(object sender, MouseEventArgs e)
        {
            if (IsMouseDown_LayoutGrid)
            {
                // we want to move it based on the position of the mouse
                moveGridControl(e);
            }
        }

        private void moveGridControl(MouseEventArgs e)
        {
            Point mousePos = e.GetPosition(OuterGrid);
            Point diff = new Point(ResizeOffset.X - mousePos.X, ResizeOffset.Y - mousePos.Y);

            if (double.IsNaN(OuterGrid.Width))
                OuterGrid.Width = OuterGrid.MinWidth;
            if (double.IsNaN(OuterGrid.Height))
                OuterGrid.Height = OuterGrid.MinHeight;
            if (Edge == cornerEdge.TopLeft)
            {
                if ((OuterGrid.Width + diff.X) >= OuterGrid.MinWidth && (OuterGrid.Width + diff.X) <= OuterGrid.MaxWidth)
                {
                    OuterGrid.Width += diff.X;
                }

                if ((OuterGrid.Height + diff.Y) >= OuterGrid.MinHeight && (OuterGrid.Height + diff.Y) <= OuterGrid.MaxHeight)
                {
                    OuterGrid.Height += diff.Y;
                }
            }
            else if (Edge == cornerEdge.Top)
            {
                if ((OuterGrid.Height + diff.Y) >= OuterGrid.MinHeight && (OuterGrid.Height + diff.Y) <= OuterGrid.MaxHeight)
                {
                    OuterGrid.Height += diff.Y;
                }
            }
            else if (Edge == cornerEdge.TopRight)
            {
                //diff.X = -diff.X;
                if ((OuterGrid.Width + diff.X) >= OuterGrid.MinWidth && (OuterGrid.Width + diff.X) <= OuterGrid.MaxWidth)
                {
                    OuterGrid.Width += diff.X;
                    //MainTranslateTransform.X += (diff.X);
                }

                if ((OuterGrid.Height + diff.Y) >= OuterGrid.MinHeight && (OuterGrid.Height + diff.Y) <= OuterGrid.MaxHeight)
                {
                    OuterGrid.Height += diff.Y;
                    //MainTranslateTransform.Y += (diff.Y);
                }
            }
            else if (Edge == cornerEdge.Left)
            {
                if ((OuterGrid.Width + diff.X) >= OuterGrid.MinWidth && (OuterGrid.Width + diff.X) <= OuterGrid.MaxWidth)
                {
                    OuterGrid.Width += diff.X;
                }
            }
            else if (Edge == cornerEdge.Right)
            {
                //diff.X = -diff.X;
                if ((OuterGrid.Width + diff.X) >= OuterGrid.MinWidth && (OuterGrid.Width + diff.X) <= OuterGrid.MaxWidth)
                {
                    OuterGrid.Width += diff.X;
                }
            }
            else if (Edge == cornerEdge.BottomLeft)
            {
                if ((OuterGrid.Width + diff.X) >= OuterGrid.MinWidth && (OuterGrid.Width + diff.X) <= OuterGrid.MaxWidth)
                {
                    OuterGrid.Width += diff.X;
                }
                //diff.Y = -diff.Y;
                if ((OuterGrid.Height + diff.Y) >= OuterGrid.MinHeight && (OuterGrid.Height + diff.Y) <= OuterGrid.MaxHeight)
                {
                    OuterGrid.Height += diff.Y;
                }
            }
            else if (Edge == cornerEdge.Bottom)
            {
                //diff.Y = -diff.Y;
                if ((OuterGrid.Height + diff.Y) >= OuterGrid.MinHeight && (OuterGrid.Height + diff.Y) <= OuterGrid.MaxHeight)
                {
                    OuterGrid.Height += diff.Y;
                }
            }
            else if (Edge == cornerEdge.BottomRight)
            {
                if ((OuterGrid.Width + diff.X) >= OuterGrid.MinWidth && (OuterGrid.Width + diff.X) <= OuterGrid.MaxWidth)
                {
                    OuterGrid.Width += diff.X;
                }

                //diff.Y = -diff.Y;
                if ((OuterGrid.Height + diff.Y) >= OuterGrid.MinHeight && (OuterGrid.Height + diff.Y) <= OuterGrid.MaxHeight)
                {
                    OuterGrid.Height += diff.Y;
                }
            }
        }

        private void LayoutGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            IsMouseDown_LayoutGrid = false;

            switch (Edge)
            {
                case cornerEdge.TopLeft:
                    Layout_TopLeftEdge.ReleaseMouseCapture();
                    break;
                case cornerEdge.Top:
                    Layout_TopEdge.ReleaseMouseCapture();
                    break;
                case cornerEdge.TopRight:
                    Layout_TopRightEdge.ReleaseMouseCapture();
                    break;
                case cornerEdge.Left:
                    Layout_LeftEdge.ReleaseMouseCapture();
                    break;
                case cornerEdge.Right:
                    Layout_RightEdge.ReleaseMouseCapture();
                    break;
                case cornerEdge.BottomLeft:
                    Layout_BottomLeftEdge.ReleaseMouseCapture();
                    break;
                case cornerEdge.Bottom:
                    Layout_BottomEdge.ReleaseMouseCapture();
                    break;
                case cornerEdge.BottomRight:
                    Layout_BottomRightEdge.ReleaseMouseCapture();
                    break;
            }
            Edge = 0;
        }
&#13;
&#13;
&#13;

0 个答案:

没有答案