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.
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.
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>
精彩评论