日期:2014-05-18 浏览次数:20749 次
// xaml部分 <Window x:Class="MarginTest.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid x:Name="outsidegrid" Background="Black" MouseLeftButtonDown="outsidegrid_MouseLeftButtonDown" MouseRightButtonDown="outsidegrid_MouseRightButtonDown"> <Grid x:Name="insidegrid" Background="LightBlue" Width="200" Height="200"> <Grid x:Name="rectGrid" Background="Red" Width="50" Height="50" HorizontalAlignment="Left" VerticalAlignment="Top"> </Grid> </Grid> </Grid> </Window>
// C#代码部分 /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void outsidegrid_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { rectGrid.Margin = new Thickness(-20, -20, 0, 0); } private void outsidegrid_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { rectGrid.Margin = new Thickness(180, 180, 0, 0); } }