2010-09-03 274 views
1

我只想問我應該怎麼把我的形象(動態)在這下面的代碼:WPF/C#:創建按鈕樣式:文字+圖片

<Style x:Key="ButtonStyler" TargetType="{x:Type Button}"> 
    <Setter Property="Background"> 
     <Setter.Value> 
      <RadialGradientBrush> 
       <RadialGradientBrush.GradientStops> 
        <GradientStopCollection> 
         <GradientStop Color="black" Offset="0" /> 
         <GradientStop Color="black" Offset="1" /> 
        </GradientStopCollection> 
       </RadialGradientBrush.GradientStops> 
      </RadialGradientBrush> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Height" Value="40" /> 
    <Setter Property="Foreground" Value="white" /> 
    <Setter Property="Grid.Row" Value="2" /> 
    <Setter Property="Grid.Column" Value="3" /> 
    <Setter Property="Content" Value="Forgot your password?" /> 
    <Setter Property="ContentTemplate" 
      Value="{DynamicResource myContentTemplate}" /> 
    <Setter Property="Margin" Value="3" /> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type Button}"> 
       <Grid> 
        <Rectangle x:Name="GelBackground" 
          Opacity="1" 
          RadiusX="9" 
          RadiusY="9" 
          Fill="{TemplateBinding Background}" 
          StrokeThickness="0.35"> 
         <Rectangle.Stroke> 
          <LinearGradientBrush StartPoint="0,0" 
            EndPoint="0,1"> 
           <LinearGradientBrush.GradientStops> 
            <GradientStopCollection> 
             <GradientStop Color="white" 
               Offset="0" /> 
             <GradientStop Color="#666666" 
               Offset="1" /> 
            </GradientStopCollection> 
           </LinearGradientBrush.GradientStops> 
          </LinearGradientBrush> 
         </Rectangle.Stroke> 
        </Rectangle> 
        <Rectangle x:Name="GelShine" 
       Margin="2,2,2,0" 
       VerticalAlignment="top" 
       RadiusX="6" 
       RadiusY="6" 
       Opacity="1" 
       Stroke="transparent" 
       Height="15px"> 
        <Rectangle.Fill> 
         <LinearGradientBrush StartPoint="0,0" EndPoint="0,1"> 
          <LinearGradientBrush.GradientStops> 
           <GradientStopCollection> 
            <GradientStop Color="#ccffffff" 
             Offset="0" /> 
            <GradientStop Color="transparent" 
             Offset="1" /> 
            </GradientStopCollection> 
           </LinearGradientBrush.GradientStops> 
          </LinearGradientBrush> 
         </Rectangle.Fill> 
        </Rectangle> 
        <ContentPresenter x:Name="GelButtonContent" 
          VerticalAlignment="center" 
          HorizontalAlignment="center" 
          Content="{TemplateBinding Content}" /> 
       </Grid> 
       <ControlTemplate.Triggers> 
        <Trigger Property="IsMouseOver" Value="true"> 
         <Setter Property="Fill" TargetName="GelBackground"> 
          <Setter.Value> 
           <RadialGradientBrush> 
            <RadialGradientBrush.GradientStops> 
             <GradientStopCollection> 
              <GradientStop Color="lime" 
                Offset="0" /> 
              <GradientStop Color="DarkGreen" 
                Offset="1" /> 
             </GradientStopCollection> 
            </RadialGradientBrush.GradientStops> 
           </RadialGradientBrush> 
          </Setter.Value> 
         </Setter> 
        </Trigger> 
        <Trigger Property="IsPressed" Value="true"> 
         <Setter Property="Fill" TargetName="GelBackground"> 
          <Setter.Value> 
           <RadialGradientBrush> 
            <RadialGradientBrush.GradientStops> 
             <GradientStopCollection> 
              <GradientStop Color="#ffcc00" 
                Offset="0" /> 
              <GradientStop Color="#cc9900" 
                Offset="1" /> 
             </GradientStopCollection> 
            </RadialGradientBrush.GradientStops> 
           </RadialGradientBrush> 
          </Setter.Value> 
         </Setter> 
        </Trigger> 
       </ControlTemplate.Triggers> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="true"> 
      <Setter Property="Foreground" Value="black " /> 
     </Trigger> 
     <Trigger Property="IsPressed" Value="true"> 
      <Setter Property="Foreground" Value="black " /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

我計劃設置圖像的文件目的地在我的代碼隱藏(C#)中。對於輸出,WPF按鈕可以同時顯示文本和圖像(放置在文本的右側)

有什麼建議嗎?

回答

4

您的模板包含一個ContentPresenter,它綁定到Button的內容。因此,您可以簡單地將圖像和/或文本設置爲按鈕的內容,並且它們將會到達那裏。

<Button Style="{StaticResource ButtonStyler}"> 
    <StackPanel Orientation="Horizontal"> 
     <Image Source="..." /> 
     <TextBlock Text="..." /> 
    </StackPanel> 
</Button> 
+0

有沒有一種方法我可以設置的.cs圖像源( C#)? – abramlimpin 2010-09-03 22:47:14

+0

是的,創建一個ImageSource實例,將其Source屬性設置爲所需的圖像,並將Image的Source屬性設置爲ImageSource實例。 – Timores 2010-09-05 10:25:15