【问题标题】:UWP convert resource override to styleUWP 将资源覆盖转换为样式
【发布时间】:2020-05-19 07:17:46
【问题描述】:

我有自定义颜色的按钮:

<Button>
<Button.Resources>
<SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="Transparent" />
</Button.Resources>
</Button>

我想把它移到样式中,所以我可以在我的按钮上设置样式,而不是把它们放在按钮 xaml 中。我发现的唯一方法是覆盖按钮模板,这是荒谬的 XAML 数量来改变一种颜色。有没有简单的方法可以做到这一点?

【问题讨论】:

  • 请注意我是在ButtonBackgroundPointerOver 等控件模板中定义的内部资源之后,而不是可以使用Background 等样式设置的控件属性。

标签: c# uwp uwp-xaml


【解决方案1】:

UWP 将资源覆盖转换为样式

ButtonBackgroundPointerOver纯色画笔,用于设置按钮指针超过状态颜色,但它不是按钮属性,所以我们不能像设置按钮背景属性一样设置它,如果你想影响特定的按钮,那就更好了方法是像这种情况下创建自定义按钮 replay ,如果你想对页面上的整个按钮产生影响,你可以在页面资源中定义它

<Page.Resources>
    <SolidColorBrush x:Key="ButtonForeground" Color="#FFFFFF"/>
    <SolidColorBrush x:Key="ButtonBackground" Color="#212121"/>
    <SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="#FFFFFF"/>
    <SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="#424242"/>
    <SolidColorBrush x:Key="ButtonBorderBrushPointerOver" Color="#FFFFFF"/>
    <SolidColorBrush x:Key="ButtonForegroundPressed" Color="#FFFFFF"/>
    <SolidColorBrush x:Key="ButtonBackgroundPressed" Color="#070707"/>
</Page.Resources>

【讨论】:

  • 除了我不想要自定义按钮,只是视觉效果。我发现定义自定义模板或类只是为了更改某些按钮的样式非常粗糙 - 我认为有更好的方法来做到这一点。
  • 您可以通过编辑按钮样式来更改ButtonBackgroundPointerOver,但您需要编辑按钮的模板,因为ButtonBackgroundPointerOver存储在VisualState
  • 但这是一个糟糕的解决方案 - 编辑按钮模板只编辑一种颜色。在我的应用程序中,我有几个按钮,具有不同的背景/前景/边框颜色,要更改它们,我需要复制按钮模板 3 次。这看起来像糟糕的设计。
  • 所以,我认为将ButtonBackgroundPointerOver 添加到Button.Resources 中很容易编辑按钮外观。
  • 是的,但我必须为应用程序中的每个按钮都这样做。然后,如果我想更改另一种颜色,我需要找到所有这些按钮,而不是更改样式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多