2012-03-29 22 views
3

我是使用Sliverlight的新手。希望可以有人幫幫我。我需要圖像按鈕來顯示訂單處於完整狀態或處理狀態。這兩者的點擊事件功能與導航同一頁面的功能相同。目前,我在App.xaml上創建了兩個客戶圖像按鈕,因爲此圖像的源不能執行「TemplateBinding」;該按鈕不具有此屬性。這是更好的方法嗎?如果是這樣,你會提供代碼或鏈接,所以我可以從中學習?謝謝。如何應用templateBinding Silverlight中的圖像按鈕

有我的代碼:

<Style x:Key="btnComplete" TargetType="Button" > 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <StackPanel> 
         <Image Height="50" Width="120" Stretch="none" Source="../images/btnComplete.png"/>        
        </StackPanel> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

回答

2

達到此目的的最簡單方法是爲該按鈕創建一個透明的控制模板,並將圖像作爲內容添加到您想要的任何按鈕。

頁面中的按鈕代碼如下所示。

<Button Height="100" Width="100" Style="{StaticResource TransparentButtonStyle}" Click="TwitterBtn_Click"> 
     <Image Height="100" Source="YourIcon.png" Width="100"/> 
    </Button> 

TransparentButtonStyle可以在App.xaml中聲明。就這樣!

<Style x:Key="TransparentButtonStyle" TargetType="Button"> 
    <Setter Property="Background" Value="Transparent" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" 
         Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style>