主要是用到grid layout 的一些方法,贴出来供以后做参考。
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
int mRow = -1;
int mColumn = -1;
MediaElement me;
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
}
private void mediaElement1_DragEnter(object sender, DragEventArgs e)
{
MessageBox.Show(sender.ToString());
}
private void play_Click(object sender, RoutedEventArgs e)
{
}
private void mediaElement1_Drop(object sender, DragEventArgs e)
{
}
private void mediaElement1_MouseLeave(object sender, MouseEventArgs e)
{
}
private void mediaElement1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (sender.GetType().ToString() == "System.Windows.Controls.Label")
{
if (mRow > -1)
{
int Column = Grid.GetColumn(this.Lable1);
int Row = Grid.GetRow(this.Lable1);
// int mRow = Grid.GetRow(this.mediaElement1);
// int mColumn = Grid.GetColumn(this.mediaElement1);
Grid.SetColumn(me, Column);
Grid.SetRow(me, Row);
Grid.SetColumn(this.Lable1, mColumn);
Grid.SetRow(this.Lable1, mRow);
mRow = -1;
}
}
}
private void mediaElement2_DragEnter(object sender, DragEventArgs e)
{
}
private void mediaElement1_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
{
me = sender as MediaElement;
mRow = Grid.GetRow(me);
mColumn = Grid.GetColumn(me);
}
}
}
xmal:
<Grid Background="AliceBlue">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Background="Aqua" Grid.Column="0" Grid.Row="0" Name="Lable1" MouseLeftButtonUp="mediaElement1_MouseLeftButtonUp" />
<MediaElement Grid.Column="1" Grid.Row="1" Source="1.wmv" Name="mediaElement1" AllowDrop="True" MouseLeftButtonUp="mediaElement1_MouseLeftButtonUp_1" />
<MediaElement Grid.Column="2" Grid.Row="2" Source="1.wmv" Name="mediaElement4" AllowDrop="True" MouseLeftButtonUp="mediaElement1_MouseLeftButtonUp_1" />
<MediaElement Grid.Column="0" Grid.Row="1" Source="trexchicken.wmv" Name="mediaElement2" AllowDrop="True" MouseLeftButtonUp="mediaElement1_MouseLeftButtonUp_1" />
<MediaElement Grid.Column="3" Grid.Row="1" Source="trexchicken.wmv" Name="mediaElement3" AllowDrop="True" MouseLeftButtonUp="mediaElement1_MouseLeftButtonUp_1" />
<Image Name="five" Grid.Column="2" Grid.Row="0" />
<Button Height="23" Grid.Row="4" Name="button1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75" Click="button1_Click">Change</Button>
<Button Height="23" Grid.Row="3" Name="play" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75" Click="play_Click">play</Button>
</Grid>
