<分区>
我想对窗口标题栏中的关闭、最小化和最大化按钮进行自定义。在某些程序中,标题栏不同,但我不想更改整个标题栏,只更改按钮。谁能帮我解决这个问题?
<分区>
我想对窗口标题栏中的关闭、最小化和最大化按钮进行自定义。在某些程序中,标题栏不同,但我不想更改整个标题栏,只更改按钮。谁能帮我解决这个问题?
最佳答案
一般来说,窗口的标题栏属于窗口的所谓非客户区。 这意味着您无法更改按钮的外观,也无法添加自定义按钮等。
为此,您需要滚动自己的窗口样式或使用为您执行此操作的库。
一些有用的教程或起点可能是 this MSDN article或本教程 how to create a custom window .
要创建自己的窗口样式,可以使用这个简单的样式作为基础:
<Style x:Key="MyCustomWindowStyle" TargetType="{x:Type Window}">
<Setter Property="WindowStyle" Value="None"/>
<Setter Property="AllowsTransparency" Value="True"/>
<Setter Property="Background" Value="White"/>
<Setter Property="BorderBrush" Value="Blue"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Window}">
<Grid Background="{TemplateBinding Background}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<!-- this displays the window title -->
<TextBlock TextAlignment="Center"
Text="{TemplateBinding Title}"/>
<StackPanel Grid.Column="1"
Orientation="Horizontal"
HorizontalAlignment="Stretch"
VerticalAlignment="Center">
<!-- the minimize button, using your own style -->
<Button Style="{StaticResource MyMinimizeButtonStyle}"
Width="20"
Height="20" />
<!-- the close button, using your own style -->
<Button Style="{StaticResource MyCloseButtonStyle}"
Width="20"
Height="20" />
</StackPanel>
<!-- this displays the actual window content -->
<ContentPresenter Grid.Row="1"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
关于c# - 如何更改标题栏按钮 WPF C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49740264/