Winrt xaml 禁用按钮时更改按钮的样式(IsEnabled=False)

Winrt xaml 禁用按钮时更改按钮的样式(IsEnabled=False),winrt-xaml,windows-10,windows-10-mobile,Winrt Xaml,Windows 10,Windows 10 Mobile,我有一个UWP问题。禁用按钮(IsEnabled=False)时,如何更改按钮的样式?以下是操作方法 <StackPanel> <Button x:Name="button" Content="Click Me" IsEnabled="{Binding IsChecked, ElementName=checkBox}"> <Interactivity:Interaction.Behaviors>

我有一个UWP问题。禁用按钮(IsEnabled=False)时,如何更改按钮的样式?

以下是操作方法

<StackPanel>

    <Button x:Name="button" Content="Click Me" 
            IsEnabled="{Binding IsChecked, ElementName=checkBox}">
        <Interactivity:Interaction.Behaviors>

            <Core:DataTriggerBehavior Binding="{Binding IsEnabled,
                ElementName=button, Mode=OneWay}" Value="True">
                <Core:ChangePropertyAction PropertyName="Opacity" Value="1"/>
            </Core:DataTriggerBehavior>

            <Core:DataTriggerBehavior Binding="{Binding IsEnabled, 
                ElementName=button, Mode=OneWay}" Value="False">
                <Core:ChangePropertyAction PropertyName="Opacity" Value=".5"/>
            </Core:DataTriggerBehavior>

        </Interactivity:Interaction.Behaviors>
    </Button>

    <CheckBox x:Name="checkBox" IsChecked="True" />

</StackPanel>

别忘了你需要参考行为SDK

祝你好运

Microsoft将(从Silverlight得知)添加到Windows通用平台。其目的是处理不同状态下控件的外观。按钮的“禁用”状态就是一个很好的例子。控件的可视状态在其ControlTemplate中定义。要自定义这些状态,最简单的方法是右键单击设计器中的按钮,选择“编辑模板”,然后选择“编辑副本…”。

这样做的目的是将该控件的默认模板复制到选定位置。 如果控件是一个按钮,您将拥有如下样式资源:

<Style x:Key="MyButtonStyle" TargetType="Button">
        <Setter Property="Background" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
        <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/>
        <Setter Property="BorderBrush" Value="{ThemeResource SystemControlForegroundTransparentBrush}"/>
        <Setter Property="BorderThickness" Value="{ThemeResource ButtonBorderThemeThickness}"/>
        <Setter Property="Padding" Value="8,4,8,4"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <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}">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="PointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseMediumLowBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseMediumLowBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightBaseHighBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <PointerDownThemeAnimation Storyboard.TargetName="RootGrid"/>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="RootGrid">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlBackgroundBaseLowBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseLowBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledTransparentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter x:Name="ContentPresenter" AutomationProperties.AccessibilityView="Raw" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" ContentTemplate="{TemplateBinding ContentTemplate}" ContentTransitions="{TemplateBinding ContentTransitions}" Content="{TemplateBinding Content}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

现在您可以看到按钮的视觉状态以及与之关联的设置器/动画。您可以手动修改它们,也可以打开Microsoft Blend 2015。它具有设计可视状态的强大功能。右键单击按钮,然后在Blend中单击“编辑模板”->“编辑当前”,您可以在“状态”选项卡(左侧)上看到控件的所有状态

当您选择“禁用”状态时,将在设计器窗口周围看到一个红色边框。左上角的文本告诉您“禁用状态录制已打开…”。这意味着对控件树中对象的每次更改都会更改控件的“禁用”状态。例如,您可以为禁用状态更改Contentpresenter的前景色。以下动画将添加到XAML中:

                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentPresenter">
                                            <DiscreteObjectKeyFrame KeyTime="0">
                                                <DiscreteObjectKeyFrame.Value>
                                                    <SolidColorBrush Color="#FF225EFF"/>
                                                </DiscreteObjectKeyFrame.Value>
                                            </DiscreteObjectKeyFrame>
                                        </ObjectAnimationUsingKeyFrames>

有关VisualSateManager以及从何处开始的更多信息,请查看


希望这有帮助

Tnx。。。这对我帮助很大。