开发者

Change the content of a button at runtime in wpf

开发者 https://www.devze.com 2023-01-03 04:55 出处:网络
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 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>
0

精彩评论

暂无评论...
验证码 换一张
取 消