2012-05-04 12:52:20 +02:00
|
|
|
|
// WPF MapControl - http://wpfmapcontrol.codeplex.com/
|
|
|
|
|
|
// Copyright © 2012 Clemens Fischer
|
|
|
|
|
|
// Licensed under the Microsoft Public License (Ms-PL)
|
|
|
|
|
|
|
2012-04-25 22:02:53 +02:00
|
|
|
|
using System.Windows;
|
|
|
|
|
|
using System.Windows.Controls;
|
|
|
|
|
|
using System.Windows.Controls.Primitives;
|
|
|
|
|
|
using System.Windows.Input;
|
|
|
|
|
|
|
|
|
|
|
|
namespace MapControl
|
|
|
|
|
|
{
|
2012-05-04 12:52:20 +02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Container class for an item in a MapItemsControl.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
|
|
|
|
|
|
[TemplateVisualState(Name = "MouseOver", GroupName = "CommonStates")]
|
|
|
|
|
|
[TemplateVisualState(Name = "Disabled", GroupName = "CommonStates")]
|
|
|
|
|
|
[TemplateVisualState(Name = "Unselected", GroupName = "SelectionStates")]
|
|
|
|
|
|
[TemplateVisualState(Name = "Selected", GroupName = "SelectionStates")]
|
|
|
|
|
|
[TemplateVisualState(Name = "NotCurrent", GroupName = "CurrentStates")]
|
|
|
|
|
|
[TemplateVisualState(Name = "Current", GroupName = "CurrentStates")]
|
2012-04-25 22:02:53 +02:00
|
|
|
|
public class MapItem : ContentControl
|
|
|
|
|
|
{
|
2012-08-22 20:21:31 +02:00
|
|
|
|
public static readonly RoutedEvent SelectedEvent = Selector.SelectedEvent.AddOwner(typeof(MapItem));
|
|
|
|
|
|
public static readonly RoutedEvent UnselectedEvent = Selector.UnselectedEvent.AddOwner(typeof(MapItem));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
public static readonly DependencyProperty IsSelectedProperty = Selector.IsSelectedProperty.AddOwner(
|
2012-08-22 20:21:31 +02:00
|
|
|
|
typeof(MapItem),
|
|
|
|
|
|
new FrameworkPropertyMetadata((o, e) => ((MapItem)o).IsSelectedPropertyChanged((bool)e.NewValue)));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
static MapItem()
|
|
|
|
|
|
{
|
2012-06-13 17:33:23 +02:00
|
|
|
|
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
|
|
|
|
|
|
typeof(MapItem), new FrameworkPropertyMetadata(typeof(MapItem)));
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
MapItemsControl.IsCurrentPropertyKey.OverrideMetadata(
|
|
|
|
|
|
typeof(MapItem),
|
|
|
|
|
|
new FrameworkPropertyMetadata((o, e) => ((MapItem)o).IsCurrentPropertyChanged((bool)e.NewValue)));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public MapItem()
|
|
|
|
|
|
{
|
|
|
|
|
|
IsEnabledChanged += IsEnabledPropertyChanged;
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public event RoutedEventHandler Selected
|
|
|
|
|
|
{
|
|
|
|
|
|
add { AddHandler(SelectedEvent, value); }
|
|
|
|
|
|
remove { RemoveHandler(SelectedEvent, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public event RoutedEventHandler Unselected
|
|
|
|
|
|
{
|
|
|
|
|
|
add { AddHandler(UnselectedEvent, value); }
|
|
|
|
|
|
remove { RemoveHandler(UnselectedEvent, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsSelected
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return (bool)GetValue(IsSelectedProperty); }
|
|
|
|
|
|
set { SetValue(IsSelectedProperty, value); }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public bool IsCurrent
|
|
|
|
|
|
{
|
2012-08-22 20:21:31 +02:00
|
|
|
|
get { return MapItemsControl.GetIsCurrent(this); }
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
public Map ParentMap
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return MapPanel.GetParentMap(this); }
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
|
|
|
|
|
|
protected override void OnMouseEnter(MouseEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnMouseEnter(e);
|
2012-08-22 20:21:31 +02:00
|
|
|
|
|
|
|
|
|
|
if (IsEnabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
VisualStateManager.GoToState(this, "MouseOver", true);
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override void OnMouseLeave(MouseEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
base.OnMouseLeave(e);
|
|
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
if (IsEnabled)
|
|
|
|
|
|
{
|
|
|
|
|
|
VisualStateManager.GoToState(this, "Normal", true);
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
private void IsEnabledPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-22 20:21:31 +02:00
|
|
|
|
if (!(bool)e.NewValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
VisualStateManager.GoToState(this, "Disabled", true);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (IsMouseOver)
|
|
|
|
|
|
{
|
|
|
|
|
|
VisualStateManager.GoToState(this, "MouseOver", true);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
VisualStateManager.GoToState(this, "Normal", true);
|
|
|
|
|
|
}
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
private void IsSelectedPropertyChanged(bool isSelected)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
if (isSelected)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
RaiseEvent(new RoutedEventArgs(SelectedEvent));
|
2012-08-22 20:21:31 +02:00
|
|
|
|
VisualStateManager.GoToState(this, "Selected", true);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2012-08-13 19:16:59 +02:00
|
|
|
|
RaiseEvent(new RoutedEventArgs(UnselectedEvent));
|
2012-08-22 20:21:31 +02:00
|
|
|
|
VisualStateManager.GoToState(this, "Unselected", true);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-08-22 20:21:31 +02:00
|
|
|
|
private void IsCurrentPropertyChanged(bool isCurrent)
|
2012-04-25 22:02:53 +02:00
|
|
|
|
{
|
2012-08-22 20:21:31 +02:00
|
|
|
|
int zIndex = Panel.GetZIndex(this);
|
|
|
|
|
|
|
|
|
|
|
|
if (isCurrent)
|
2012-08-13 19:16:59 +02:00
|
|
|
|
{
|
2012-08-22 20:21:31 +02:00
|
|
|
|
Panel.SetZIndex(this, zIndex | 0x40000000);
|
|
|
|
|
|
VisualStateManager.GoToState(this, "Current", true);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2012-08-22 20:21:31 +02:00
|
|
|
|
Panel.SetZIndex(this, zIndex & ~0x40000000);
|
|
|
|
|
|
VisualStateManager.GoToState(this, "NotCurrent", true);
|
2012-04-25 22:02:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|