2010-06-10 44 views
2

我在wpf(实际上是silverlight)应用程序中有一个按钮。 我想在运行时改变这个按钮的内容,以便为它添加一个图像(例如,如果内容是“按钮一”,我希望内容变成:包含image1 +原始按钮文本的stackpanel)。在运行时更改按钮的内容wpf

请帮忙。

回答

2

检查:

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. 
1

把图象相反的,隐藏和使用BooleanToVisibilityConverter表现出来。 ShowImage是一个布尔属性,您设置为true/false来显示/隐藏图像。

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