【问题标题】:Text color change when button is disabled in xamarin在 xamarin 中禁用按钮时文本颜色更改
【发布时间】:2016-10-07 14:40:22
【问题描述】:

我正在从事 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 来检查它

【问题讨论】:

  • 在任何情况下,您都将其设置为 White
  • 但在这两种情况下我都想要白色的文本颜色。
  • 当它禁用时,它的颜色会变成黑色。我不想要
  • 使用自定义渲染器:stackoverflow.com/a/49193488/1039935

标签: xamarin uwp


【解决方案1】:

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

在您的情况下,查找已禁用情节提要并更改

&lt;ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground"&gt;

到你喜欢的颜色或主题资源...

【讨论】:

  • “它抛出 Can't resolve TemplateProperty on Button”异常
  • 哦,我看到你已经使用了颜色!属性是画笔,所以先改变它!
  • Dude... 例如在您的代码中: 你把 WHITE 放在了不起作用的 BorderBRUSH 中
猜你喜欢
  • 2018-04-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多