工具栏按钮样式被忽略? [英] Toolbar button styles get ignored?

查看:39
本文介绍了工具栏按钮样式被忽略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have buttons on a toolbar in WPF.

当我执行 XAML 时:

When I do the XAML:

<ToolBar.Resources>
   <Style TargetType="{x:Type Button}">
      <Setter Property="Width" Value="21"></Setter>
      <Setter Property="Height" Value="21"></Setter>
   </Style>
</ToolBar.Resources>

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

None of the buttons on the toolbar set their sizes accordingly.

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

I have to go to each button and manually set their widths and heights to the desired values.

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

Any idea why the Style on the toolbar does not work?

推荐答案

出现这种情况是因为 ToolBarToolBar.ButtonStyleKey 标识的样式应用于按钮,而不是将它们保留使用默认样式.(这就是为什么即使提高了默认样式,工具栏中的按钮也是扁平的.)参考.

This occurs because ToolBar applies the style identified by ToolBar.ButtonStyleKey to buttons, instead of leaving them with the default style. (That's why buttons in Toolbars are flat even though the default style is raised.) Reference.

你需要劫持"这个样式,而不是默认样式:

You need to "hijack" this style, instead of the default style:

<ToolBar.Resources>
  <Style x:Key="{x:Static ToolBar.ButtonStyleKey}" TargetType="Button">
    <Setter Property="Width" Value="100" />
  </Style>
</ToolBar.Resources>

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

Note the x:Key in the Style declaration.

这篇关于工具栏按钮样式被忽略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆