|
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
namespace SelectionAreaSample
{
public sealed class SelectionAreaItem : ContentControl
{
#region Public Events
public static readonly RoutedEvent SelectedEvent = Selector.SelectedEvent.AddOwner(typeof(SelectionAreaItem));
public event RoutedEventHandler Selected
{
add
{
base.AddHandler(SelectedEvent, value);
}
remove
{
base.RemoveHandler(SelectedEvent, value);
}
}
public static readonly RoutedEvent UnselectedEvent = Selector.UnselectedEvent.AddOwner(typeof(SelectionAreaItem));
public event RoutedEventHandler Unselected
{
add
{
base.AddHandler(UnselectedEvent, value);
}
remove
{
base.RemoveHandler(UnselectedEvent, value);
}
}
#endregion Public Events
#region Public Properties
public static readonly DependencyProperty IsSelectedProperty = Selector.IsSelectedProperty.AddOwner(typeof(SelectionAreaItem), new FrameworkPropertyMetadata(false, OnIsSelectedChanged));
public bool IsSelected
{
get
{
return (bool)GetValue(IsSelectedProperty);
}
set
{
SetValue(IsSelectedProperty, value);
}
}
private static void OnIsSelectedChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
var item = target as SelectionAreaItem;
if ((bool)e.NewValue)
{
item.RaiseEvent(new RoutedEventArgs(Selector.SelectedEvent, item));
}
else
{
item.RaiseEvent(new RoutedEventArgs(Selector.UnselectedEvent, item));
}
}
#endregion Public Properties
#region Internal Properties
internal SelectionArea ParentSelectionArea
{
get
{
return ItemsControl.ItemsControlFromItemContainer(this) as SelectionArea;
}
}
#endregion Internal Properties
#region Protected Methods
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
var parent = ParentSelectionArea;
if (parent != null)
{
parent.NotifyItemClicked(this);
}
e.Handled = true;
}
protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters)
{
if (VisualTreeHelper.GetDescendantBounds(this).Contains(hitTestParameters.HitPoint))
{
return new PointHitTestResult(this, hitTestParameters.HitPoint);
}
return null;
}
protected override GeometryHitTestResult HitTestCore(GeometryHitTestParameters hitTestParameters)
{
var geometry = new RectangleGeometry(VisualTreeHelper.GetDescendantBounds(this));
return new GeometryHitTestResult(this, geometry.FillContainsWithDetail(hitTestParameters.HitGeometry));
}
#endregion Protected Methods
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.