英文:
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 = "";
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);
}
英文:
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.
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);
}```
</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:
<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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论