📜  wpf 圆形按钮 - C# (1)

📅  最后修改于: 2023-12-03 15:21:15.878000             🧑  作者: Mango

WPF Circula Button - C#

WPF provides a variety of controls, including buttons, to create user interfaces. However, circular buttons are not natively supported. This tutorial will show you how to create a circular button in WPF using C#.

Adding a Circular Button

To add a circular button to your WPF application, follow these steps:

  1. Create a new WPF project in Visual Studio.
  2. Open the MainWindow.xaml file.
  3. Add the following XML code inside the Grid element:
<Button Width="100" Height="100" Content="Click Me" Click="Button_Click">
    <Button.Template>
        <ControlTemplate TargetType="Button">
            <Ellipse Width="100" Height="100" Fill="LightGray"/>
        </ControlTemplate>
    </Button.Template>
</Button>
  1. This XAML code creates a button with a width and height of 100 pixels and a content of "Click Me". The button also has an event handler named "Button_Click".
  2. The Template property sets the control template for the button.
  3. The ControlTemplate element defines the appearance of the button. In this case, it is a 100x100 ellipse filled with light gray.
Adding Functionality to the Circular Button

To add functionality to the button, you must handle its Click event. To do this, follow these steps:

  1. Open the MainWindow.xaml.cs file.
  2. Add the following method:
private void Button_Click(object sender, RoutedEventArgs e)
{
    MessageBox.Show("Button clicked!");
}
  1. This method displays a message box with the text "Button clicked!" when the button is clicked.
Conclusion

By following the steps outlined in this tutorial, you can easily create a circular button in your WPF application using C#. You can also customize the appearance and add functionality to the button as needed to meet the requirements of your application.