简体   繁体   English

在Windows Phone 8中按下时更改按钮的图像

[英]Change the image of buttons when pressed in Windows Phone 8

I set image for button with a code like this 我用这样的代码为按钮设置图像

<Button x:Name="btToBackgroundPage" Click="toBackground_Click" Width="250" Height="100" BorderThickness="0">
    <Image Source="/Assets/Image/A.png" Stretch="Fill"/>
</Button>

So when i click it, i want to change the Image within button to something like "B.png" 因此,当我单击它时,我想将“按钮内的图像”更改为“ B.png”

I know i can do it by using pressed event of button and change source of image , but my app have many buttons like that and I just want to know if is there anyway to set that in xaml ? 我知道我可以通过使用按下按钮的事件并更改图像源来做到这一点,但是我的应用程序有很多类似的按钮,我只是想知道是否有必要在xaml中进行设置?

I don't think you could do that in xaml itself, because somehow you got to have an event handler for your image click event to change the image source of the button . 我不认为您可以在xaml本身中做到这一点,因为您必须以某种方式为image单击事件提供事件处理程序才能更改button的图像源。

How to change the background image of button on runtime? 如何在运行时更改按钮的背景图像? would be helpful. 会有所帮助。

Hope it helps! 希望能帮助到你!

This may look little bit complex. 这看起来有点复杂。 But It works fine. 但是它工作正常。

XAML: XAML:

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="11,0,12,0" Background="Black">

            <Button x:Name="b1" Content="Button" HorizontalAlignment="Left" Margin="208,421,0,0" VerticalAlignment="Top" MouseEnter="MouseEnter" MouseLeave="MouseLeave" Tag="b1">
                <Button.Background>
                    <ImageBrush x:Name="b1bg"  ImageSource="/Assets/WP_20140902_001.jpg" />
                </Button.Background>
                <Button.Template>
                    <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.TargetName="ContentContainer" 
                                       Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PressedHighlightBackground" 
                                       Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
                                       Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" 
                                       Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
                                       Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
                                       Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}"  
            BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" 
            Background="{TemplateBinding Background }" Margin="{StaticResource PhoneTouchTargetOverhang}" >
                                <Border x:Name="PressedHighlightBackground" Background="Transparent">
                                    <ContentControl x:Name="ContentContainer" Foreground="{TemplateBinding Foreground}" 
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}" 
                      Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                                </Border>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
            <Button x:Name="b2" Content="Button" HorizontalAlignment="Left" Margin="71,421,0,0" VerticalAlignment="Top" MouseEnter="MouseEnter" MouseLeave="MouseLeave" Tag="b2">
                <Button.Background>
                    <ImageBrush x:Name="b2bg"  ImageSource="/Assets/WP_20140902_001.jpg"/>
                </Button.Background>
                <Button.Template>
                    <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.TargetName="ContentContainer" 
                                       Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="PressedHighlightBackground" 
                                       Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
                                       Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Disabled">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" 
                                       Storyboard.TargetProperty="Foreground">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
                                       Storyboard.TargetProperty="BorderBrush">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" 
                                       Storyboard.TargetProperty="Background">
                                                <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" />
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}"  
            BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" 
            Background="{TemplateBinding Background }" Margin="{StaticResource PhoneTouchTargetOverhang}" >
                                <Border x:Name="PressedHighlightBackground" Background="Transparent">
                                    <ContentControl x:Name="ContentContainer" Foreground="{TemplateBinding Foreground}" 
                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}" 
                      Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/>
                                </Border>
                            </Border>
                        </Grid>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </Grid>

CS:: CS ::

private void MouseLeave(object sender, MouseEventArgs e)
{
    Button temp = sender as Button;

    var actionString = temp.Tag;
    var value = this.FindName(actionString + "bg");
    ((ImageBrush)value).ImageSource = new BitmapImage(new Uri("/Assets/WP_20140902_001.jpg", UriKind.Relative));
}

private void MouseEnter(object sender, MouseEventArgs e)
{
    Button temp = sender as Button;
    var actionString = temp.Tag;
    var value= this.FindName(actionString + "bg");
    ((ImageBrush)value).ImageSource = new BitmapImage(new Uri("/Assets/splash_480_720.png", UriKind.Relative));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM