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

时间:2018-07-17 08:00:26

标签: xaml uwp xaml-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中使用)并在多种资源样式中使用它?

1 个答案:

答案 0 :(得分:-1)

您可以使用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>     

相关问题