2010-06-10 47 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>