如何在UWP中更改按钮背景

时间:2017-07-23 18:04:01

标签: c# xaml uwp uwp-xaml

我想点击按钮更改按钮背景。但是,我不想使用click事件来更改背景。

我已尝试使用WPF并使用以下代码实现它:

<Style x:Key="ButtonStyle" TargetType="Button">
    <Setter Property="Background" 
            Value="White"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type Button}">
                <Grid>
                    <Border CornerRadius="3" 
                            BorderBrush="DarkGray" 
                            BorderThickness="1" 
                            Background="{TemplateBinding Background}" />
                    <ContentPresenter HorizontalAlignment="Center" 
                                      VerticalAlignment="Center" />
                </Grid>
                <ControlTemplate.Triggers >
                    <Trigger Property="IsFocused" 
                             Value="True">
                        <Setter Property="BorderThickness" 
                                Value="2"/>
                        <Setter Property="FontWeight" 
                                Value="Bold"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Button Content="All" 
        Height="20" 
        Width="50" 
        Margin="2" 
        Style="{StaticResource ButtonStyle}" />

有人可以建议如何在UWP中实现相同的功能吗?

1 个答案:

答案 0 :(得分:2)

  

我想点击按钮更改按钮背景。但是,我不想使用点击事件来改变背景。

根据您的要求,您可以通过使用XamlBehaviors来实现它。例如:

<Button>
    <Interactivity:Interaction.Behaviors>
        <Core:EventTriggerBehavior EventName="Click">
            <Core:ChangePropertyAction PropertyName="Background">
                <Core:ChangePropertyAction.Value>
                    <SolidColorBrush Color="Red"/>
                </Core:ChangePropertyAction.Value>
            </Core:ChangePropertyAction>
        </Core:EventTriggerBehavior>
    </Interactivity:Interaction.Behaviors>
</Button>

它是开源模型和主机GitHub上的所有行为代码将允许更快地解决新功能和修复。

相关问题