<ListBox Name="map" SelectionChanged="map_SelectionChanged" Grid.Row="1">
<ListBox.Resources>
<Style TargetType="ListBoxItem">
<Setter Property="Canvas.Top" Value="{Binding Path=Top}"/>
<Setter Property="Canvas.Left" Value="{Binding Path=Left}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Border MouseLeftButtonDown="item_MouseLeftButtonDown"
MouseLeftButtonUp="item_MouseLeftButtonUp"
MouseMove="item_MouseMove">
<Image Source="{Binding Path=ImageSource}" Stretch="Uniform" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.Resources>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
public class Entity : INotifyPropertyChanged
{
private double top;
public double Top
{
get { return top; }
set
{
top = value;
OnPropertyChanged("Top");
}
}
private double left;
public double Left
{
get { return left; }
set
{
left = value;
OnPropertyChanged("Left");
}
}
public BitmapImage ImageSource { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}
private bool moving = false;
private Entity selectedItem = null;
private Point offset;
public Page()
{
InitializeComponent();
// アイコン情報のリストを作ってね
map.ItemsSource = new List<Entity>();
}
private void item_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
this.moving = true;
Border control = sender as Border;
if (control != null)
{
this.offset = e.GetPosition(control);
}
}
private void item_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
this.moving = false;
}
private void item_MouseMove(object sender, MouseEventArgs e)
{
if (this.moving && this.selectedItem != null)
{
Point position = e.GetPosition(map);
this.selectedItem.Left = position.X - this.offset.X;
this.selectedItem.Top = position.Y - this.offset.Y;
}
}
private void map_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
this.selectedItem = map.SelectedValue as Entity;
controlPanel.DataContext = this.selectedItem;
}
ドラッグ&ドロップはおまけみたいなもん。
0 件のコメント:
コメントを投稿