Reputation: 1391
I need to create a custom combo box that display shapes. I created the shapes by extending the Shape class, and implementing the DefiningGeometry function like this:
public abstract class MyShape : Shape
{
public static readonly DependencyProperty SizeProperty = DependencyProperty.Register("Size", typeof(Double), typeof(MapShape));
public static readonly DependencyProperty RotationAngleProperty = DependencyProperty.Register("RotationAngle", typeof(Double), typeof(MapShape), new PropertyMetadata(0.0d));
public double Size
{
get { return (double)this.GetValue(SizeProperty); }
set { this.SetValue(SizeProperty, value); }
}
public double RotationAngle
{
get { return (double)this.GetValue(RotationAngleProperty); }
set { this.SetValue(RotationAngleProperty, value); }
}
protected override Geometry DefiningGeometry
{
get
{ return null; }
}
}
I can extend that class and create any other shape I want. for instance, I have one that looks like an arrow:
public class Arrow : MyShape
{
public Arrow() {
}
protected override Geometry DefiningGeometry
{
get
{
double oneThird = this.Size / 3;
double twoThirds = (this.Size * 2) / 3;
double oneHalf = this.Size / 2;
Point p1 = new Point(0.0d, oneThird);
Point p2 = new Point(0.0d, twoThirds);
Point p3 = new Point(oneHalf, twoThirds);
Point p4 = new Point(oneHalf, this.Size);
Point p5 = new Point(this.Size, oneHalf);
Point p6 = new Point(oneHalf, 0);
Point p7 = new Point(oneHalf, oneThird);
List<PathSegment> segments = new List<PathSegment>(3);
segments.Add(new LineSegment(p1, true));
segments.Add(new LineSegment(p2, true));
segments.Add(new LineSegment(p3, true));
segments.Add(new LineSegment(p4, true));
segments.Add(new LineSegment(p5, true));
segments.Add(new LineSegment(p6, true));
segments.Add(new LineSegment(p7, true));
List<PathFigure> figures = new List<PathFigure>(1);
PathFigure pf = new PathFigure(p1, segments, true);
figures.Add(pf);
RotateTransform rt = new RotateTransform(this.RotationAngle);
Geometry g = new PathGeometry(figures, FillRule.EvenOdd, rt);
return g;
}
}
}
I can add this shapes on XAML or code and they work just fine.
Now, those shapes are displayed on a graphics object somewhere in my form which is no relevant. The requirement I have is for the shapes on the graphic object in the form to be changed by the client from a ComboBox. So, basically I need to display the shapes inside the combo box as well. I really don't need to use these classes I am showing here, it is just for clarification I add them to this note. But I do need to customize the combobox to show shapes in the items. One way I thought is using the ControlTemplate, Any other ideas, code, readings? Thanks!
Upvotes: 1
Views: 297
Reputation: 44028
If I understood, what you want can be achieved by customizing the ItemTemplate
property of the ComboBox
.
<ComboBox ...>
<ComboBox.ItemTemplate>
<DataTemplate>
<!-- Whatever UI -->
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
Upvotes: 1