XAML-Map-Control/MapControl/MapItem.cs

141 lines
4.6 KiB
C#
Raw Normal View History

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
{
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(
typeof(MapItem),
new FrameworkPropertyMetadata((o, e) => ((MapItem)o).IsSelectedPropertyChanged((bool)e.NewValue)));
2012-04-25 22:02:53 +02:00
static MapItem()
{
FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
typeof(MapItem), new FrameworkPropertyMetadata(typeof(MapItem)));
2012-04-25 22:02:53 +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
{
get { return MapItemsControl.GetIsCurrent(this); }
2012-04-25 22:02:53 +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);
if (IsEnabled)
{
VisualStateManager.GoToState(this, "MouseOver", true);
}
2012-04-25 22:02:53 +02:00
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
if (IsEnabled)
{
VisualStateManager.GoToState(this, "Normal", true);
}
2012-04-25 22:02:53 +02:00
}
private void IsEnabledPropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
2012-04-25 22:02:53 +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
}
private void IsSelectedPropertyChanged(bool isSelected)
2012-04-25 22:02:53 +02:00
{
if (isSelected)
2012-04-25 22:02:53 +02:00
{
RaiseEvent(new RoutedEventArgs(SelectedEvent));
VisualStateManager.GoToState(this, "Selected", true);
2012-04-25 22:02:53 +02:00
}
else
{
RaiseEvent(new RoutedEventArgs(UnselectedEvent));
VisualStateManager.GoToState(this, "Unselected", true);
2012-04-25 22:02:53 +02:00
}
}
private void IsCurrentPropertyChanged(bool isCurrent)
2012-04-25 22:02:53 +02:00
{
int zIndex = Panel.GetZIndex(this);
if (isCurrent)
{
Panel.SetZIndex(this, zIndex | 0x40000000);
VisualStateManager.GoToState(this, "Current", true);
2012-04-25 22:02:53 +02:00
}
else
{
Panel.SetZIndex(this, zIndex & ~0x40000000);
VisualStateManager.GoToState(this, "NotCurrent", true);
2012-04-25 22:02:53 +02:00
}
}
}
}