以编程方式更改按钮的图像 [英] Change image of button programitically
问题描述
您好,
我有一个拥有图像的按钮...问题是,当我点击该按钮时,我想在c#中实际更改按钮的图像...
任何人都可以建议更改图像的正确解决方案......下面的代码是我的xaml
< pre lang =xml> < 按钮 内容 = Go < span class =code-attribute> 高度 = 23 名称 = btnViewInventory 宽度 =
< Button.Template >
< ; ControlTemplate >
< 图像 来源 = .. \Images\View.png VerticalAlignment = 中心 高度 = 30 宽度 = 25 名称 = imgGroupAdd > < / Image >
< / ControlTemplate >
< / Button.Template >
< /按钮 > 点击后更改按钮样式表
看看这个链接
http://social.msdn.microsoft.com/Forums/vstudio/en-US/05edb0bb-13bc -4c85-acb1-1245add255e8 / how-can-i-add-an-image-background-for-button-in-aspnet [ ^ ]
http://www.nullskull.com/q/10164749/set-background-image-to-an-ltaspbutton--gt-- controlhow-is-it-possible.aspx [ ^ ]
看看这个链接这可能会对你有所帮助
http://stackoverflow.com/questions/15627123/adding-an-image-inside-a-button-programmatically [ ^ ]
http://stackoverflow.com/questions/5971300/programmatically-changing-button-icon-in-wpf [ ^ ]
我终于解决了我的自我...
Xaml
< 按钮 高度 = 23 名称 = btnGoLineitem 宽度 = 40 BorderBrush = < span class =code-keyword>透明 Grid.Row = 5 Grid.Column = 4 HorizontalAlignment = Left 点击 = btnGoLineitem_Click >
< Button.Template >
< ControlTemplate >
< 图片 < span class =code-attribute> Source = .. \Images\NewItem.png 名称 = imgLineItemAdd > < / Image >
< / ControlTemplate >
< / Button.Template >
首先获取您在设计模式中定义的控制模板(xaml)
ControlTemplate ct = btnGoLineitem.Template;
访问xaml端存在的图像标记
图像btnImage =(图像)ct.FindName(imgLineItemAdd,btnGoLineitem);
现在只需更改图像源
btnImage.Source = new BitmapImage(new Uri( ../../Change.png,UriKind.RelativeOrAbsolute));
Hello,
I am having a button which posses an image... problem is that when I click that button I want to change the image of the button pragmatically in c#...
Can any one suggest the correct solution to change the image... Below code is my xaml
<Button Content="Go" Height="23" Name="btnViewInventory" Width="40" Grid.Row="1" Cursor="Hand" ToolTip="View group details " HorizontalAlignment="Left" Grid.Column="4" Click="btnViewInventory_Click" >
<Button.Template>
<ControlTemplate>
<Image Source="..\Images\View.png" VerticalAlignment="Center" Height="30" Width="25" Name="imgGroupAdd" ></Image>
</ControlTemplate>
</Button.Template>
</Button>
Change button style sheet after click
have a look of this link
http://social.msdn.microsoft.com/Forums/vstudio/en-US/05edb0bb-13bc-4c85-acb1-1245add255e8/how-can-i-add-an-image-background-for-button-in-aspnet[^]
http://www.nullskull.com/q/10164749/set-background-image-to-an-ltaspbutton--gt--controlhow-is-it-possible.aspx[^]
have a look of this link this may help you
http://stackoverflow.com/questions/15627123/adding-an-image-inside-a-button-programmatically[^]
http://stackoverflow.com/questions/5971300/programmatically-changing-button-icon-in-wpf[^]
Hi,
I solved it my self yar finally....
Xaml
<Button Height="23" Name="btnGoLineitem" Width="40" BorderBrush="Transparent" Grid.Row="5" Grid.Column="4" HorizontalAlignment="Left" Click="btnGoLineitem_Click" > <Button.Template> <ControlTemplate> <Image Source="..\Images\NewItem.png" Name="imgLineItemAdd" ></Image> </ControlTemplate> </Button.Template>
First Get access of control template which you have defined in design mode(xaml)
ControlTemplate ct = btnGoLineitem.Template; Get access of the image tag that is present in the xaml side Image btnImage = (Image)ct.FindName("imgLineItemAdd", btnGoLineitem);
Now just change the image source
btnImage.Source = new BitmapImage(new Uri("../../Change.png", UriKind.RelativeOrAbsolute));
这篇关于以编程方式更改按钮的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!