WPF 中的半圆按钮

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

half circle button in wpf

问题

  1. 我目前正在使用C#代码创建一个WPF界面,其中包含一列按钮。我想让这些按钮呈现半圆形,但我不知道如何做。非常感谢您所有人的帮助。
  2. [类似于这样](https://i.stack.imgur.com/6kQWi.png)

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

  1. buttonList[i].Content = &quot;&quot;;
  2. buttonList[i].Foreground = Brushes.White;
  3. buttonList[i].Background = Brushes.Transparent;
  4. buttonList[i].BorderBrush = Brushes.Black;
  5. buttonList[i].Name = &quot;Btn&quot; + i;
  6. buttonList[i].Click += new RoutedEventHandler(Btn_Click);
  7. forzaGrid.Children.Add(buttonList[i]);
  8. Grid.SetRow(buttonList[i], 0);
  9. 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

  1. buttonList = new Button[7];
  2. for (int i = 0; i &lt; 7; i++)
  3. {
  4. buttonList[i] = new Button();
  5. buttonList[i].Content = &quot;&quot;;
  6. buttonList[i].Foreground = Brushes.White;
  7. buttonList[i].Background = Brushes.Transparent;
  8. buttonList[i].BorderBrush = Brushes.Black;
  9. buttonList[i].Name = &quot;Btn&quot; + i;
  10. buttonList[i].Click += new RoutedEventHandler(Btn_Click);
  11. forzaGrid.Children.Add(buttonList[i]);
  12. Grid.SetRow(buttonList[i], 0);
  13. Grid.SetColumn(buttonList[i], i);
  14. }```
  15. </details>
  16. # 答案1
  17. **得分**: 1
  18. 你可以直接在XAML中定义你的按钮和半圆:
  19. ```xml
  20. <Button Width="40"
  21. Height="20"
  22. Padding="0"
  23. Background="Transparent">
  24. <Button.Template>
  25. <ControlTemplate TargetType="Button">
  26. <ContentPresenter Content="{TemplateBinding Content}" />
  27. </ControlTemplate>
  28. </Button.Template>
  29. <Path Fill="White"
  30. Width="40"
  31. Height="21"
  32. Stroke="Black"
  33. StrokeThickness="1" HorizontalAlignment="Center" VerticalAlignment="Center">
  34. <Path.Data>
  35. <CombinedGeometry GeometryCombineMode="Intersect">
  36. <CombinedGeometry.Geometry1>
  37. <EllipseGeometry RadiusX="18"
  38. RadiusY="18"
  39. Center="20,20" />
  40. </CombinedGeometry.Geometry1>
  41. <CombinedGeometry.Geometry2>
  42. <RectangleGeometry Rect="0,0,40,20" />
  43. </CombinedGeometry.Geometry2>
  44. </CombinedGeometry>
  45. </Path.Data>
  46. </Path>
  47. </Button>

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

英文:

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

  1. &lt;Button Width=&quot;40&quot;
  2. Height=&quot;20&quot;
  3. Padding=&quot;0&quot;
  4. Background=&quot;Transparent&quot;&gt;
  5. &lt;Button.Template&gt;
  6. &lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
  7. &lt;ContentPresenter Content=&quot;{TemplateBinding Content}&quot; /&gt;
  8. &lt;/ControlTemplate&gt;
  9. &lt;/Button.Template&gt;
  10. &lt;Path Fill=&quot;White&quot;
  11. Width=&quot;40&quot;
  12. Height=&quot;21&quot;
  13. Stroke=&quot;Black&quot;
  14. StrokeThickness=&quot;1&quot; HorizontalAlignment=&quot;Center&quot; VerticalAlignment=&quot;Center&quot;&gt;
  15. &lt;Path.Data&gt;
  16. &lt;CombinedGeometry GeometryCombineMode=&quot;Intersect&quot;&gt;
  17. &lt;CombinedGeometry.Geometry1&gt;
  18. &lt;EllipseGeometry RadiusX=&quot;18&quot;
  19. RadiusY=&quot;18&quot;
  20. Center=&quot;20,20&quot; /&gt;
  21. &lt;/CombinedGeometry.Geometry1&gt;
  22. &lt;CombinedGeometry.Geometry2&gt;
  23. &lt;RectangleGeometry Rect=&quot;0,0,40,20&quot; /&gt;
  24. &lt;/CombinedGeometry.Geometry2&gt;
  25. &lt;/CombinedGeometry&gt;
  26. &lt;/Path.Data&gt;
  27. &lt;/Path&gt;
  28. &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:

确定