XAML - 从资源中定义的样式绑定到资源中定义的值

XAML - Bind to value defined in resources from style defined in resources

我有一个具有已定义资源的用户控件。 这是一些用于说明的代码。

<UserControl.Resources>
    <SolidColorBrush x:Key="foregroundColor" Color="Red"/>

    <Style x:Key="buttonFontIconStyle" TargetType="FontIcon">
        <Setter Property="FontFamily" Value="Segoe MDL2 Assets"></Setter>
        <Setter Property="Foreground" Value="{Binding ???}"></Setter>
    </Style>

    <Style x:Key="menuItemLabelStyle" TargetType="TextBlock">
        <Setter Property="VerticalAlignment" Value="Center"></Setter>
        <Setter Property="Foreground" Value="{Binding ???}"></Setter>
    </Style>     
</UserControl.Resources>

现在,我希望为 buttonFontIconStyle、menuItemLabelStyle(以及许多其他)使用前景色中定义的值。是否有可能以某种方式绑定到资源中资源的值,或者有没有办法指定一次颜色(最好在 xaml 中)并在多种资源样式中使用它?

您可以使用 StaticResource :

<Style x:Key="buttonFontIconStyle" TargetType="FontIcon">
    <Setter Property="FontFamily" Value="Segoe MDL2 Assets"></Setter>
    <Setter Property="Foreground" Value="{StaticResource foregroundColor}"></Setter>
</Style>

<Style x:Key="menuItemLabelStyle" TargetType="TextBlock">
    <Setter Property="VerticalAlignment" Value="Center"></Setter>
    <Setter Property="Foreground" Value="{StaticResource foregroundColor}"></Setter>
</Style>