WPF 中的半圆按钮

huangapple go评论79阅读模式
英文:

half circle button in wpf

问题

我目前正在使用C#代码创建一个WPF界面,其中包含一列按钮。我想让这些按钮呈现半圆形,但我不知道如何做。非常感谢您所有人的帮助。

[类似于这样](https://i.stack.imgur.com/6kQWi.png)

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

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

}

英文:

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 &lt; 7; i++)
            {
                buttonList[i] = new Button();

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


</details>


# 答案1
**得分**: 1

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

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

你可以将这个内容封装到UserControl中,这样将更容易添加到你的Grid中。

英文:

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

&lt;Button Width=&quot;40&quot;
            Height=&quot;20&quot;
            Padding=&quot;0&quot;
            Background=&quot;Transparent&quot;&gt;

        &lt;Button.Template&gt;
            &lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
                &lt;ContentPresenter Content=&quot;{TemplateBinding Content}&quot; /&gt;
            &lt;/ControlTemplate&gt;
        &lt;/Button.Template&gt;
    
        &lt;Path Fill=&quot;White&quot;
                Width=&quot;40&quot;
                Height=&quot;21&quot;
                Stroke=&quot;Black&quot;
                StrokeThickness=&quot;1&quot; HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;
            &lt;Path.Data&gt;
                &lt;CombinedGeometry GeometryCombineMode=&quot;Intersect&quot;&gt;
                    &lt;CombinedGeometry.Geometry1&gt;
                        &lt;EllipseGeometry RadiusX=&quot;18&quot;
                                            RadiusY=&quot;18&quot;
                                            Center=&quot;20,20&quot; /&gt;
                    &lt;/CombinedGeometry.Geometry1&gt;
                    &lt;CombinedGeometry.Geometry2&gt;
                        &lt;RectangleGeometry Rect=&quot;0,0,40,20&quot; /&gt;
                    &lt;/CombinedGeometry.Geometry2&gt;
                &lt;/CombinedGeometry&gt;
            &lt;/Path.Data&gt;
        &lt;/Path&gt;
&lt;/Button&gt;

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

huangapple
  • 本文由 发表于 2023年6月6日 02:31:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定