Xaml 选择按钮时如何临时突出显示按钮的背景

Xaml 选择按钮时如何临时突出显示按钮的背景,xaml,windows-phone-7,button,windows-phone-8,Xaml,Windows Phone 7,Button,Windows Phone 8,我有几个按钮在我的页面上水平排列,当用户选择一个按钮时,我希望背景变成某种颜色,并保持这种状态,直到按下另一个按钮。我创建了一个样式来突出显示按钮的背景,但我不知道如何在按下另一个按钮之前保持背景突出显示。我已将按钮样式2应用于所有按钮 MainPage.xaml <Style x:Key="ButtonStyle2" TargetType="Button"> <Setter Property="Background" Value="Transparent"/&

我有几个按钮在我的页面上水平排列,当用户选择一个按钮时,我希望背景变成某种颜色,并保持这种状态,直到按下另一个按钮。我创建了一个样式来突出显示按钮的背景,但我不知道如何在按下另一个按钮之前保持背景突出显示。我已将
按钮样式2
应用于所有按钮

MainPage.xaml

<Style x:Key="ButtonStyle2" TargetType="Button">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/>
        <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/>
        <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/>
        <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/>
        <Setter Property="Padding" Value="10,5,10,6"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="0" Background="{TemplateBinding Background}" CornerRadius="0" Margin="0">
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="0" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

          ...

<ListBoxItem toolkit:TiltEffect.IsTiltEnabled="True" Width="72">
                <Button x:Name="Button1" Tap="Button1_Tap" Style="{StaticResource ButtonStyle2}">
                    <Button.Content>
                        <Image Source="/Assets/Icons/appbar.settings.png"/>
                    </Button.Content>
                </Button>
            </ListBoxItem>

            ...

            <ListBoxItem toolkit:TiltEffect.IsTiltEnabled="True" Width="72">
                <Button x:Name="Button3" Tap="Button3_Tap" Style="{StaticResource ButtonStyle2}">
                    <Button.Content>
                        <Image Source="/Assets/Icons/appbar.view.png"/>
                    </Button.Content>
                </Button>
            </ListBoxItem>

...
...

<代码> > p>您通过更改样式来采取正确的方法,但是考虑另一个控件而不是按钮。

提议的行为

您所描述的似乎是一组相互排斥的按钮。您有一组按钮,其中一个处于活动状态。当其处于激活状态时,其他按钮将被禁用。当然你是在运行代码的时候激活了按钮,但在我看来你真的想要一种方法来创建一组互斥的按钮

您可以尝试以这种方式使按钮控件工作,但Windows Phone中已有一些控件可以这样做<强>无线电按钮是你应该考虑的一个。

退税

当然,单选按钮看起来不像传统的按钮,所以您可能没有考虑使用它们

但在XAML中,您可以将RadioButton设置为与普通按钮类似的样式,或者将图像放在RadioButton内容或任何适当的UI上

如果你能接受标准的外观,你就完蛋了。否则,将您的风格调整为RadioButton,而不是Button,手机会跟踪按下哪个RadioButton


Matthias Shapiro。

您可以将您的样式放置在app.xaml资源文件中。并通过按钮点击事件上的c#代码应用它

btn.Style=App.Current.Resources[“StyleKey”]作为样式


这里btn是您在xaml中的按钮名称。

使用单一样式是不可能的,因为您不能让一个按钮根据另一个按钮的功能保持其状态管理,按钮的可视状态是有限的,并以原子方式应用于按钮(按下按钮后,无法根据周围的其他按钮切换按钮的禁用状态)

使用不同的背景制作两种样式,并在单击按钮或

您可以在StackPanel和TextBlock的帮助下创建一个虚拟对象

在xaml中类似这样的东西

<StackPanel Name="stkButton1" Tap="stkButton1_Tap" Height="50" Width="225" Background="Blue">
<TextBlock Text="Button 1" Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Name="stkButton2" Tap="stkButton2_Tap" Height="50" Width="225" Background="Gray">
<TextBlock Text="Button 2" Margin="0,10,0,0" Foreground="White" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</StackPanel>
private void stkButton2_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
           stkButton1.Background = new SolidColorBrush(Colors.Gray);
            stkButton2.Background = new SolidColorBrush(Colors.Blue);
        }

        private void stkButton1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {

            stkButton2.Background = new SolidColorBrush(Colors.Gray);
            stkButton1.Background = new SolidColorBrush(Colors.Blue);
        }