wpf 如何更改 MahApps 中所有命令按钮的前景色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21576846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 10:43:58  来源:igfitidea点击:

How to change the foreground color of all command buttons in MahApps

wpfxamlbuttonforegroundmahapps.metro

提问by user3049133

Is there a way to change buttons foreground in MetroWindow? I have even tried to override the IronicallyNamedChromelessButtonStyle but the foreground color was still the same.

有没有办法改变 MetroWindow 中的按钮前景?我什至试图覆盖 IronicallyNamedChromelessButtonStyle 但前景色仍然相同。

Edit: The buttons are in the Window Bar (e.g Close, Minimize, Maximize).

编辑:按钮位于窗口栏中(例如关闭、最小化、最大化)。

回答by user3049133

After a deep dive into the MahApps codе... Here is the source which is responsible for the buttons: https://github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/Themes/MetroWindow.xamlIf you look carefully, you will notice that every style has triggers that override the style foreground with hard-coded "White":

在深入了解 MahApps 代码之后......这是负责按钮的源代码:https: //github.com/MahApps/MahApps.Metro/blob/master/MahApps.Metro/Themes/MetroWindow.xaml如果你仔细看,你会注意到每个样式都有触发器,用硬编码的“白色”覆盖样式前景:

<Style.Triggers>
        <DataTrigger Binding="{Binding ShowTitleBar, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}}"
                     Value="True">
            <Setter Property="Foreground"
                    Value="White" />
        </DataTrigger>
        <DataTrigger Binding="{Binding ShowTitleBar, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:MetroWindow}}}"
                     Value="False">
            <Setter Property="Background"
                    Value="Transparent" />
        </DataTrigger>
    </Style.Triggers>

My solution was to override all necessary style triggers:

我的解决方案是覆盖所有必要的样式触发器:

<Style TargetType="{x:Type MahControls:WindowButtonCommands}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding ShowTitleBar, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MahControls:MetroWindow}}}"
                     Value="True">
            <Setter Property="Foreground"
                    Value="{StaticResource IdealForegroundColorBrush}" />
        </DataTrigger>
    </Style.Triggers>
</Style>

Hope this will help anyone in my case. Special thanks to @Rui and @Sankarann for the ideas and help. If anyone have a better solution, please share it.

希望这会对我的情况有所帮助。特别感谢@Rui 和@Sankaran 的想法和帮助。如果有人有更好的解决方案,请分享。

回答by Rui

You can use an implicit style. An implicit style is a style that does not have a "key", for example, if you add this style in Application.Resources it will affect all buttons in your application:

您可以使用隐式样式。隐式样式是一种没有“键”的样式,例如,如果您在 Application.Resources 中添加此样式,它将影响应用程序中的所有按钮:

<Application.Resources>
  <Style TargetType="Button">
    <Setter Property="Foreground" Value="Blue"/>
  </Style>
</Application.Resources>

You can set it in, for example, a Grid's Resources and it will affect only the Buttons inside that Grid.

例如,您可以在网格的资源中设置它,它只会影响该网格内的按钮。

回答by Tino Mclaren

Yes your right the colours are applied in appbar_ canvas via

是的,您的权利通过在 appbar_canvas 中应用颜色

Fill="{DynamicResource BlackBrush}"

So as your not in control of BlackBrush you cant really apply a SolidColorBrush to it as some other control in the MahApps Library will overwite your setting.

因此,由于您无法控制 BlackBrush,因此您无法真正将 SolidColorBrush 应用到它,因为 MahApps 库中的其他一些控件会覆盖您的设置。

You need to point NuGet to the Loose resources file (so you get an Icons.xaml file to play with locally)

您需要将 NuGet 指向 Loose 资源文件(这样您就可以在本地使用 Icons.xaml 文件)

Copy the appbar icons you want to change color (maybe create a resource dictionary called MyIcons.xaml and save them in there, adding MyIcons.xaml to your App.xaml MergedDictionaries)

复制要更改颜色的应用栏图标(可能创建一个名为 MyIcons.xaml 的资源字典并将它们保存在那里,将 MyIcons.xaml 添加到您的 App.xaml MergedDictionaries)

Then in MyIcons.xaml define your icon (with its new colour too) :

然后在 MyIcons.xaml 中定义您的图标(也使用新颜色):

<SolidColorBrush x:Key="IconBrushOrange" Color="Orange" />

<Canvas  x:Key="my_connecting" All other fields...>
    <Path Stretch="Fill" Fill="{StaticResource IconBrushOrange}" All other fields....  />
</Canvas>

Then in your UI :

然后在您的用户界面中:

<Rectangle Width="20" Height="20">
   <Rectangle.Fill>
       <VisualBrush Stretch="Fill" Visual="{StaticResource my_connecting}"  />
   </Rectangle.Fill>
</Rectangle>