1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
//custom panel
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace WpfButtonTemplateCirculaire
{
public class EllipticalPanel : Panel
{
public static readonly DependencyProperty ItemWidthProperty = DependencyProperty.Register(
"ItemWidth", typeof(double), typeof(EllipticalPanel), new FrameworkPropertyMetadata(0.0,
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsArrange));
public static readonly DependencyProperty ItemHeightProperty = DependencyProperty.Register(
"ItemHeight", typeof(double), typeof(EllipticalPanel), new FrameworkPropertyMetadata(0.0,
FrameworkPropertyMetadataOptions.AffectsMeasure | FrameworkPropertyMetadataOptions.AffectsArrange));
public static readonly DependencyProperty UseFerrisWheelLayoutProperty = DependencyProperty.Register(
"UseFerrisWheelLayout", typeof(bool), typeof(EllipticalPanel), new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsArrange));
public bool UseFerrisWheelLayout
{
get { return (bool) GetValue(UseFerrisWheelLayoutProperty); }
set { SetValue(UseFerrisWheelLayoutProperty, value); }
}
public double ItemWidth
{
get { return (double)GetValue(ItemWidthProperty); }
set { SetValue(ItemWidthProperty, value); }
}
public double ItemHeight
{
get { return (double)GetValue(ItemHeightProperty); }
set { SetValue(ItemHeightProperty, value); }
}
protected override Size MeasureOverride(Size constraint)
{
if (constraint.Width == double.PositiveInfinity || constraint.Height == double.PositiveInfinity)
return Size.Empty;
foreach (UIElement child in InternalChildren)
{
child.Measure(new Size(ItemWidth, ItemHeight));
}
return constraint;
}
protected override Size ArrangeOverride(Size finalSize)
{
// Calculate radius
double radiusX = (finalSize.Width - ItemWidth) * 0.5;
double radiusY = (finalSize.Height - ItemHeight) * 0.5;
double count = InternalChildren.Count;
// Sector angle between items
double deltaAngle = 2 * Math.PI / count;
// Center of the ellipse
Point center = new Point(finalSize.Width / 2, finalSize.Height / 2);
for (int i = 0; i < count; i++)
{
UIElement child = InternalChildren[i];
// Calculate position
double angle = i * deltaAngle;
double x = center.X + radiusX * Math.Cos(angle) - ItemWidth / 2;
double y = center.Y + radiusY * Math.Sin(angle) - ItemHeight / 2;
if (UseFerrisWheelLayout)
{
child.RenderTransform = null;
}
else
{
child.RenderTransformOrigin = new Point(0.5, 0.5);
child.RenderTransform = new RotateTransform(angle * 180 / Math.PI);
}
child.Arrange(new Rect(x, y, ItemWidth, ItemHeight));
}
return finalSize;
}
}
}
//custom button
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls.Primitives;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Controls;
namespace WpfButtonTemplateCirculaire
{
public class CustomTextButton : Button
{
public CustomTextButton()
{
this.ItemsSource = new ObservableCollection<Char>();
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text",
typeof(string), typeof(CustomTextButton),
new UIPropertyMetadata(null,OnTxtChanged));
private static void OnTxtChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
{
CustomTextButton custBtn= d as CustomTextButton;
string s = args.NewValue as String;
if (! string.IsNullOrEmpty(s))
{
/* conversion en maj...*/
s = s.ToUpper();
/* met à jour ItemsSource...*/
custBtn.ItemsSource = new ObservableCollection<Char>(s.ToCharArray());
}
}
// une prop attache ItemsSource "pretee" par ItemsControl grace a AddOwner
public ObservableCollection<Char> ItemsSource
{
get { return (ObservableCollection<Char>)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
public static readonly DependencyProperty ItemsSourceProperty =
ItemsControl.ItemsSourceProperty.AddOwner( typeof(CustomTextButton) );
}
} |
Partager