wpf为自定义title添加关闭按钮
时间: 2024-05-06 19:19:55 浏览: 14
要为WPF自定义标题添加关闭按钮,可以使用以下步骤:
1.在Window的模板中添加一个Button元素,作为关闭按钮。
```xml
<ControlTemplate TargetType="{x:Type Window}">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="0">
<TextBlock Text="{TemplateBinding Title}" DockPanel.Dock="Left"/>
<Button Content="X" Click="CloseButton_Click" DockPanel.Dock="Right"/>
</DockPanel>
<AdornerDecorator Grid.Row="1">
<ContentPresenter/>
</AdornerDecorator>
</Grid>
</ControlTemplate>
```
2.在Window的代码中添加CloseButton_Click事件处理程序,以关闭窗口。
```c#
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
```
3.最后,将窗口的模板设置为自定义模板。
```xml
<Window Template="{StaticResource MyWindowTemplate}">
<!-- 窗口内容 -->
</Window>
```
这样就可以为WPF自定义标题添加关闭按钮了。
相关推荐
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)