WPF中的半圆按钮

bkkx9g8r  于 2023-06-07  发布在  其他
关注(0)|答案(1)|浏览(253)

即时通讯currently做一个wpf接口与一个列表的按钮,从他的c#代码,我想使这些按钮像半个圆圈,但我不知道如何做到这一点。谢谢大家的进步。
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);
            }```
vc9ivgsu

vc9ivgsu1#

您可以直接在XAML中定义按钮和半圆:

<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>

您可以将其 Package 到UserControl中,这将使其更容易添加到Grid中。

相关问题