views:

1883

answers:

3

I dont think this is quite possible, but its worth a shot to see what you guys say. I am trying to create a half-elliptical button in C# (not XAML, that might through another curve ball). If I wanted a pre-determined size for the buttons, I would just use images or something like that, but thats not the case since the size changes. Here is more detail:

  1. I have an ellipse with x-radius and y-radius (or width and height, respectfully multiplied by 2).

  2. I want two button to fill the entire ellipse, each taking up one half of the ellipse.

  3. I dont want rectangular button that extend beyond the ellipse and get clipped to the parent, I want actual elliptical buttons, except only one-half of an ellipse per button.

  4. If this cant be accomplished using buttons, but using some other control, then I'd like that control to be able to act like a button.

Any help or advice or pointers would greatly help.

+3  A: 

Before I answer this, I'd love to know WHY you have to avoid XAML, in a WPF application? You would almost certainly be indirectly using XAML anyway, so why not use it - making your button should then be a piece of cake! That's exactly what it's for !

This is like trying to make a house with sticky-tape when you are standing next to bricks and mortar! :)

pezi_pink_squirrel
The size of the overall ellipse changes. Say someone put a line of code like myEllipse.xRadius = 40; The width of each button becomes 40 essentially at the center most part of the button. If something like that would work using pure XAML, then that would work.
Nick
YES! That would work. XAML is very good for this layout stuff. You could quite easily do this, just expose a Radius property, then in the set, apply radius*2 to your base control width and the control will auto-resize if you have it setup correctly! :)
pezi_pink_squirrel
Okay, so how would I get two buttons to make an elliptical shape, not necessarily an ellipse object? And then as the desired ellipse shape size changes, the buttons to change accordingly.
Nick
Drawing the half-ellipses together is the hardest bit really. You could even draw one with a line through it and work out which one was clicked by the mouse location. I will show you an example if I ever get home from work, unless someone else gets in there first!
pezi_pink_squirrel
Yeah I thought about just comparing the mouse location to see which half was clicked, but I dont know where to begin. I tried create an ellipse object and setting a visual brush fill, but the buttons came out rectangular and non-clickable.
Nick
I found a way to create a elliptical button through the control template of a button. If I use this example, I think the best way to do it is to deceive the user into believing there are two separate buttons by the way the gradient is shaded. Compare the mouse pos and fix the gradient accordingly.
Nick
A: 

Use XAML, seriously XAML may look daunting at first but it's nothing like the headache you're creating for yourself by trying to do this purely in code behind.

Custom WPF Button Style

Step-by-Step Introduction to styling with Blend

XAML Level 100

Graeme Bradbury
Okay so how would I do it in the XAML?
Nick
A: 

You'll have to add additional triggers (like IsPressed), but this should give you a pretty good idea:

<Button Height="30" Width="30">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate>
                        <Path Name="HalfEllipse" Stroke="Black" StrokeThickness="1" Fill="Blue">
                            <Path.Data>
                                <PathGeometry>
                                    <PathFigure IsFilled="True" StartPoint="0,0">
                                        <PolyBezierSegment Points="5,30 25,30 30,0" />
                                    </PathFigure>
                                </PathGeometry>
                            </Path.Data>
                        </Path>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="HalfEllipse" Property="Fill">
                                    <Setter.Value>
                                        <SolidColorBrush Color="Green"/>
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Button.Style>
</Button>

See this page for more info: http://www.codeproject.com/KB/WPF/glassbuttons.aspx

Pakman