tags:

views:

44

answers:

2

I have a button in a wpf (silverlight actually) application. I want to change the content of this button at runtime in order to add an image to it (for example, if content was "button one", i want the content to become: stackpanel containing image1 + original button text).

Please help.

+2  A: 

Hi Zee99,

Check this:

var sp = new StackPanel();
var img = new Image() {Source = ...}
sp.Children.Add(img);
sp.Children.Add("Hello world");
btn.Content = sp; // btn - is the name of your button.
Anvaka
+1  A: 

Instead of adding the image, hide and show it using BooleanToVisibilityConverter. ShowImage is a bool property that you set to true/false to show/hide the image.

<Button>
    <StackPanel Orientation="Horizontal">
        <Image Visibility="{Binding Path=ShowImage, Converter={StaticResource BooleanToVisibilityConverter}}"/>
        <TextBlock Margin="5,0,0,0" Text="button one" />
    </StackPanel>
</Button>
Wallstreet Programmer