按下前景颜色按下

时间:2016-08-05 08:06:23

标签: c# xaml windows-phone-8.1 windows-phone uwp

我正在编写Windows 10 UWP应用程序。我创建了一个按钮,其中包含带有两个文本块的stackpanel。我想在按下时更改这些文本块的前景。

我修改了默认按钮样式。因此,单击此按钮时,BorderBrush正在更改,但文本块的前景不会更改。

<Button Command="{Binding}" Style="{StaticResource Button|SecondaryPurpleButtonStyle}" >
  <Button.Content>  
    <StackPanel Orientation="Horizontal">
      <TextBlock Margin="4" Style="{StaticResource TextBlock|MenuIconTextBlockStyle}" Foreground="{StaticResource Color|BrandLogoPurpleSolidColorBrush}"
      Text="&#xE734;" />
      <TextBlock Style="{StaticResource ThemedBodyTextBlockStyle}"  Foreground="{StaticResource Color|BrandLogoPurpleSolidColorBrush}"
      Margin="4"  Text="Add to Shortlist" />     
    </StackPanel>  
  </Button.Content>
</Button>

样式:

<Style x:Key="Button|SecondaryPurpleButtonStyle"
       TargetType="Button">
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="Foreground" Value="{StaticResource Color|BrandLogoPurpleSolidColorBrush}" />
    <Setter Property="BorderBrush" Value="{StaticResource Color|BrandLogoPurpleSolidColorBrush}" />
    <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}" />
    <Setter Property="Padding" Value="8,4,8,4" />
    <Setter Property="HorizontalAlignment" Value="Left" />

    <Setter Property="HorizontalContentAlignment" Value="Center" />

    <Setter Property="VerticalAlignment" Value="Center" />
    <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
    <Setter Property="FontWeight" Value="Normal" />
    <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
    <Setter Property="UseSystemFocusVisuals" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Grid x:Name="RootGrid"
                      Background="{TemplateBinding Background}">
                    <ContentPresenter x:Name="ContentPresenter"
                                      HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                      VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                      AutomationProperties.AccessibilityView="Raw"
                                      BorderBrush="{TemplateBinding BorderBrush}"
                                      BorderThickness="0.7"
                                      Content="{TemplateBinding Content}"



                                      ContentTemplate="{TemplateBinding ContentTemplate}"
                                      ContentTransitions="{TemplateBinding ContentTransitions}"
                                      Padding="{TemplateBinding Padding}" />
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal">
                                <Storyboard>
                                    <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="PointerOver">
                                <Storyboard>
                                    <!--<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource BrandLogoOrangePointerChangeSolidColorBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>-->
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid"
                                                                   Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{StaticResource Color|BrandLogoLightestPurplePointerChangeSolidColorBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <!--<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource OLXLogoOrangeSolidColorBrush}"/>
                                    </ObjectAnimationUsingKeyFrames>-->
                                    <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid"
                                                                   Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{StaticResource Color|BrandLogoPurpleSolidColorBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                   Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{StaticResource Color|BrandLogoPurpleSolidColorBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                   Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="White" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <PointerDownThemeAnimation Storyboard.TargetName="RootGrid" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid"
                                                                   Storyboard.TargetProperty="Background">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{ThemeResource SystemControlBackgroundBaseLowBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                   Storyboard.TargetProperty="Foreground">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{ThemeResource SystemControlDisabledBaseLowBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                   Storyboard.TargetProperty="BorderBrush">
                                        <DiscreteObjectKeyFrame KeyTime="0"
                                                                Value="{ThemeResource SystemControlDisabledTransparentBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:1)

在您的XAML代码中,您已明确将#Set up a PSCredential: $yourPassword = ConvertTo-SecureString "<Your Password>" -AsPlainText –Force $yourCredential = New-Object -TypeName pscredential –ArgumentList "<Your UserName>", $yourPassword # Then use that credential to log in to your Azure account Login-AzureRmAccount -Credential $yourCredential -ServicePrincipal –TenantId <Your TenantId> 的{​​{1}}属性设置为TextBlock。所以前景不会改变。

要确保文本块的前景可以更改为Foreground,请删除{StaticResource Color|BrandLogoPurpleSolidColorBrush}中的BorderBrush属性,并确保未设置Foreground属性以TextBlock的风格。

Foreground

这样TextBlock中的<Button Command="{Binding}" Style="{StaticResource Button|SecondaryPurpleButtonStyle}" > <Button.Content> <StackPanel Orientation="Horizontal"> <TextBlock Margin="4" Style="{StaticResource TextBlock|MenuIconTextBlockStyle}" Text="&#xE734;" /> <TextBlock Style="{StaticResource ThemedBodyTextBlockStyle}" Margin="4" Text="Add to Shortlist" /> </StackPanel> </Button.Content> </Button> 将使用TextBlock的{​​{1}},Button.Content可以在不同的ContentPresenter中更改。