首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在运行时更改按钮的背景图像?

如何在运行时更改按钮的背景图像?
EN

Stack Overflow用户
提问于 2011-06-13 13:54:26
回答 3查看 16K关注 0票数 4

我被一个问题缠住了。我想在运行时更改按钮的背景图像。我得到了更改颜色的解决方案,但我想要更改图像。

代码如下

代码语言:javascript
运行
复制
public void buttonCase(object sender, RoutedEventArgs e)
{
    Uri uri = null;
    var image = new ImageBrush();
    if (((App)App.Current).appControler.m_Mode == Controller.textMode.Letters)
    {
        ((App)App.Current).appControler.buttonCase(sender, e);
        switch (((App)App.Current).appControler.m_case)
        {
        case Controller.caseMode.Upper:
            b0.FontSize = b1.FontSize = b2.FontSize = b3.FontSize = b4.FontSize = b5.FontSize = b6.FontSize = b7.FontSize
            = b8.FontSize = b9.FontSize = bCornerLower.FontSize = 30.0;
            uri = new Uri(@"/SourceCode;component/Images/Lower_Case_p.png", UriKind.Relative);
            image.ImageSource = new BitmapImage(uri);
            btnCase.Background = image;
            break;
        case Controller.caseMode.Lower:
            b0.FontSize = b1.FontSize = b2.FontSize = b3.FontSize = b4.FontSize = b5.FontSize = b6.FontSize = b7.FontSize
            = b8.FontSize = b9.FontSize = bCornerLower.FontSize = 40.0;
            uri = new Uri(@"/SourceCode;component/Images/Case_p.png", UriKind.Relative);
            image.ImageSource = new BitmapImage(uri);
            btnCase.Background = image;
            break;
        }
    }
}  
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-13 13:58:40

像这样的东西?

代码语言:javascript
运行
复制
var brush = new ImageBrush();
brush.ImageSource = new BitmapImage(new Uri(@"Images/myImage.png", UriKind.Relative)); 
myButton.Background = brush;

编辑我制作了一个包含两个图像的测试应用程序。我在点击按钮时切换图像,它就能工作。

代码语言:javascript
运行
复制
private bool flag;

private void button1_Click(object sender, RoutedEventArgs e)
{
    flag = !flag;

    var uriString = flag ? @"Images/logo.png" : @"Images/icon.png";
    myButton.Background = new ImageBrush 
        { ImageSource = new BitmapImage(new Uri(uriString, UriKind.Relative)) };
}
票数 13
EN

Stack Overflow用户

发布于 2012-06-08 21:50:11

代码语言:javascript
运行
复制
private void button_Click(object sender, EventArgs e)
        {
           button.Image=System.Drawing.Image.FromFile("image.png");
        }

试试这个..

票数 0
EN

Stack Overflow用户

发布于 2013-11-11 17:27:33

利用VisualStates进行这种UI状态切换。您的代码将如下所示:

代码语言:javascript
运行
复制
public void buttonCase(object sender, RoutedEventArgs e)
{
    if (((App)App.Current).appControler.m_Mode == Controller.textMode.Letters)
    {
        ((App)App.Current).appControler.buttonCase(sender, e);
        switch (((App)App.Current).appControler.m_case)
        {
        case Controller.caseMode.Upper:
            VisualStateManager.GoToState( this, "UpperCase", true );
            break;
        case Controller.caseMode.Lower:
            VisualStateManager.GoToState( this, "LowerCase", true );
            break;
        }
    }
}  

您的xaml将如下所示:

代码语言:javascript
运行
复制
<UserControl
x:Class="SilverlightApplication2.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:system="clr-namespace:System;assembly=mscorlib">
<UserControl.Resources>
    <ControlTemplate x:Key="MySpecialToggleButton" TargetType="ToggleButton">
        <Grid>
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CheckStates">
                    <VisualState x:Name="Checked">
                        <Storyboard>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="LowerCaseIcon">
                                <DiscreteObjectKeyFrame KeyTime="0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <Visibility>Visible</Visibility>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames>
                            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Storyboard.TargetName="UpperCaseIcon">
                                <DiscreteObjectKeyFrame KeyTime="0">
                                    <DiscreteObjectKeyFrame.Value>
                                        <Visibility>Collapsed</Visibility>
                                    </DiscreteObjectKeyFrame.Value>
                                </DiscreteObjectKeyFrame>
                            </ObjectAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualState>
                    <VisualState x:Name="Unchecked"/>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
            <Image x:Name="UpperCaseIcon" Source="/SilverlightApplication2;component/Images/Lower_Case_p.png" Visibility="Visible"/>
            <Image x:Name="LowerCaseIcon" Source="/SilverlightApplication2;component/Images/Case_p.png" Visibility="Collapsed"/>
        </Grid>
    </ControlTemplate>
</UserControl.Resources>

<StackPanel>
    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="LetterCaseStates">
            <VisualState x:Name="UpperCase"/>
            <VisualState x:Name="LowerCase">
                <Storyboard>
                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="FontSize" Storyboard.TargetName="bCornerLower">
                        <DiscreteObjectKeyFrame KeyTime="0">
                            <DiscreteObjectKeyFrame.Value>
                                <system:Double>40</system:Double>
                            </DiscreteObjectKeyFrame.Value>
                        </DiscreteObjectKeyFrame>
                    </ObjectAnimationUsingKeyFrames>
                </Storyboard>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

    <ToggleButton x:Name="btnCase" Click="buttonCase" Template="{StaticResource MySpecialToggleButton}"/>
    <Button x:Name="bCornerLower" FontSize="30"/><!-- this one is the styling master, all other buttons follow its styling -->
    <Button x:Name="b0" FontSize="{Binding ElementName=bCornerLower, Path=FontSize}"/>
    <Button x:Name="b1" FontSize="{Binding ElementName=bCornerLower, Path=FontSize}"/>
    <Button x:Name="b2" FontSize="{Binding ElementName=bCornerLower, Path=FontSize}"/>
    <Button x:Name="b3" FontSize="{Binding ElementName=bCornerLower, Path=FontSize}"/>
    <Button x:Name="b4" FontSize="{Binding ElementName=bCornerLower, Path=FontSize}"/>
    <Button x:Name="b5" FontSize="{Binding ElementName=bCornerLower, Path=FontSize}"/>
    <Button x:Name="b6" FontSize="{Binding ElementName=bCornerLower, Path=FontSize}"/>
    <Button x:Name="b7" FontSize="{Binding ElementName=bCornerLower, Path=FontSize}"/>
    <Button x:Name="b8" FontSize="{Binding ElementName=bCornerLower, Path=FontSize}"/>
    <Button x:Name="b9" FontSize="{Binding ElementName=bCornerLower, Path=FontSize}"/>
</StackPanel>

我编写代码是为了回答这里的一个类似问题:toggle button with different images

我知道定义VisualStates可能看起来相当麻烦,但最终你可以让你的代码变得非常整洁,而不是切换所有ui零碎的视觉外观。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6327074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档