在xamarin中禁用按钮时文本颜色更改

时间:2016-10-06 09:39:33

标签: xamarin uwp

我正在使用UWP应用程序。我有按钮,当禁用时,textcolor会变为黑色。我试图使用像这样的触发器将其改为白色

 <Button x:Name="btnEnter" Grid.Column="2" Text="Enter" BackgroundColor="#092B73" TextColor="White" Clicked="btnEnter_Clicked" >
        <Button.Triggers>
          <Trigger TargetType="Button"
               Property="IsEnabled" Value="true">
            <Setter Property="TextColor" Value="White" />
          </Trigger>
          <Trigger TargetType="Button"
               Property="IsEnabled" Value="false">
            <Setter Property="TextColor" Value="White" />
          </Trigger>
        </Button.Triggers>
        </Button>

但它不起作用。请帮助找到错误的地方

更新

 <?xml version="1.0" encoding="utf-8" ?>
 <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="StockDispatchApp.Views.TestDisableButton">
<ContentPage.Resources>
    <Style TargetType="Button">
    <Setter Property="Template">
        <Setter.Value>
        <ControlTemplate TargetType="Button">
            <Grid x:Name="RootGrid" Background="{TemplateBinding Background}">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                <VisualState x:Name="Normal">
                    <Storyboard>
                    <PointerUpThemeAnimation Storyboard.TargetName="RootGrid" />
                    </Storyboard>
                </VisualState>

                <VisualState x:Name="Disabled">
                    <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="RootGrid"
                                                        Storyboard.TargetProperty="Background">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="Blue" />
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                        Storyboard.TargetProperty="Foreground">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="Red" />
                    </ObjectAnimationUsingKeyFrames>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                        Storyboard.TargetProperty="BorderBrush">
                        <DiscreteObjectKeyFrame KeyTime="0" Value="White" />
                    </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <ContentPresenter x:Name="ContentPresenter"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Content="{TemplateBinding Content}"
                                ContentTransitions="{TemplateBinding ContentTransitions}"
                                ContentTemplate="{TemplateBinding ContentTemplate}"
                                Padding="{TemplateBinding Padding}"
                                HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
                                AutomationProperties.AccessibilityView="Raw"/>
            </Grid>
        </ControlTemplate>
        </Setter.Value>
    </Setter>
    </Style>
</ContentPage.Resources>
 <StackLayout >
<Button x:Name="btn1" Text="Button1"  Clicked="btn1_clicked"></Button>
<Button x:Name="btn2" Text="Button2" BackgroundColor="Green"   TextColor="White"></Button>
</StackLayout>
</ContentPage>

在btn1中点击我刚刚禁用了btn2来检查它

1 个答案:

答案 0 :(得分:1)

复制/粘贴MSDN(https://msdn.microsoft.com/en-us/library/windows/apps/mt299109.aspx)上列出的默认按钮样式,并根据自己的喜好进行调整。

在您的情况下,查找“已禁用”故事板并更改

<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">

使用您喜欢的颜色或主题资源......