Search code examples
c#wpfxaml

half circle button in wpf


im currrently doing a wpf interface with a list of button made from he c# code, i want to make these button like half a circle but i dont know how to do it. thanks you all in advancement.

something like this

buttonList = new Button[7];
            for (int i = 0; i < 7; i++)
            {
                buttonList[i] = new Button();

                buttonList[i].Content = "";
                buttonList[i].Foreground = Brushes.White;
                buttonList[i].Background = Brushes.Transparent;
                buttonList[i].BorderBrush = Brushes.Black;
                buttonList[i].Name = "Btn" + i;
                buttonList[i].Click += new RoutedEventHandler(Btn_Click);
                forzaGrid.Children.Add(buttonList[i]);
                Grid.SetRow(buttonList[i], 0);
                Grid.SetColumn(buttonList[i], i);
            }```

Solution

  • You can define your button and half circle in XAML directly:

    <Button Width="40"
                Height="20"
                Padding="0"
                Background="Transparent">
    
            <Button.Template>
                <ControlTemplate TargetType="Button">
                    <ContentPresenter Content="{TemplateBinding Content}" />
                </ControlTemplate>
            </Button.Template>
        
            <Path Fill="White"
                    Width="40"
                    Height="21"
                    Stroke="Black"
                    StrokeThickness="1" HorizontalAlignment="Center" VerticalAlignment="Center">
                <Path.Data>
                    <CombinedGeometry GeometryCombineMode="Intersect">
                        <CombinedGeometry.Geometry1>
                            <EllipseGeometry RadiusX="18"
                                                RadiusY="18"
                                                Center="20,20" />
                        </CombinedGeometry.Geometry1>
                        <CombinedGeometry.Geometry2>
                            <RectangleGeometry Rect="0,0,40,20" />
                        </CombinedGeometry.Geometry2>
                    </CombinedGeometry>
                </Path.Data>
            </Path>
    </Button>
    

    You can wrap this into UserControl which will make it easier to add to your Grid.