使用样式设置工具栏按钮大小

The*_*ies 6 wpf styles toolbar

我在WPF的工具栏上有按钮.

当我做XAML时:

<ToolBar.Resources>
   <Style TargetType="{x:Type Button}">
      <Setter Property="Width" Value="21"></Setter>
      <Setter Property="Height" Value="21"></Setter>
   </Style>
</ToolBar.Resources>
Run Code Online (Sandbox Code Playgroud)

工具栏上的所有按钮都没有相应地设置其大小.

我必须转到每个按钮并手动将其宽度和高度设置为所需的值.

知道为什么工具栏上的样式不起作用?

ito*_*son 9

这是因为工具栏应用,而不是留给他们的默认样式由ToolBar.ButtonStyleKey确定了按钮的样式.(这就是为什么即使提出默认样式,工具栏中的按钮也是平的.)你需要"劫持"这种风格,而不是默认风格:

<ToolBar.Resources>
  <Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="Button">
    <Setter Property="Width" Value="100" />
  </Style>
</ToolBar.Resources>
Run Code Online (Sandbox Code Playgroud)

请注意样式声明中的x:Key.