2012-07-30 223 views
0

我試圖使用控件模板來自定義WPF中的按鈕。 我有以下問題:1。 鼠標懸停按鈕的顏色不會更改爲紅色 2.當按下它給出了一個擺動的效果WPF按鈕樣式

下面

是XAML: - >

    <Setter.Value> 
         <ControlTemplate TargetType="Button"> 
          <Border Name="border" 
          BorderThickness="1" 
          Padding="4,2" 
          BorderBrush="DarkGray" 
          CornerRadius="3" 
          Background="{StaticResource ButtonDarkColorBrush}"> 
           <Grid > 

            <ContentPresenter HorizontalAlignment="Center" 
             VerticalAlignment="Center" Name="content" Style="{StaticResource ShadowStyle}"/> 
           </Grid> 

          </Border> 
          <ControlTemplate.Triggers> 
           <Trigger Property="IsMouseOver" Value="True"> 
            <!--<Setter TargetName="border" Property="BorderBrush" Value="#FF4788c8" />--> 
            <Setter Property="Foreground" Value="#FF4788c8" /> 
            <Setter Property="Background" Value="Red" /> 
           </Trigger> 


           <Trigger Property="IsPressed" Value="True"> 
            <Setter TargetName="content" Property="RenderTransform" > 
             <Setter.Value> 
              <TranslateTransform Y="0.1" /> 
             </Setter.Value> 
            </Setter> 
           </Trigger> 
           <Trigger Property="IsDefaulted" Value="True"> 
            <Setter TargetName="border" Property="BorderBrush" Value="#FF282828" /> 
           </Trigger> 
           <Trigger Property="IsFocused" Value="True"> 
            <Setter TargetName="border" Property="BorderBrush" Value="#FF282828" /> 
           </Trigger> 
           <Trigger Property="IsEnabled" Value="False"> 
            <Setter TargetName="border" Property="Opacity" Value="0.7" /> 
            <Setter Property="Foreground" Value="Gray" /> 
           </Trigger> 
          </ControlTemplate.Triggers> 

         </ControlTemplate> 
        </Setter.Value> 

       </Setter> 
      </Style> 

請告知 感謝

+0

爲什麼你不粘貼一個完整的樣式xaml?低質量的問題很快就被標記了! – HichemSeeSharp 2012-07-31 00:10:14

+0

我的壞事很快就完成了 – melspring 2012-07-31 00:34:15

+0

第一個問題:給你的MouseOver背景設置者一個TargetName =「border」。 – LPL 2012-07-31 00:36:27

回答

2

Melspring嗨試試這個代碼,它爲我工作。

<Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="Button"> 
        <Border Name="border" 
       BorderThickness="2" 
       Padding="4,2" 
       BorderBrush="{DynamicResource BlueGradient2}" 
       CornerRadius="5" 
       Background="{TemplateBinding Background}"> 
         <Grid > 
          <ContentPresenter 
          HorizontalAlignment="Center" 
          VerticalAlignment="Center" 
          Name="content"/> 
         </Grid> 
        </Border> 
        <ControlTemplate.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Background" Value="Red"/> 
         </Trigger> 
         <Trigger Property="IsEnabled" Value="False"> 
          <Setter Property="Background" Value="Red"/> 
          <Setter Property="BorderBrush" Value="Red"/> 
         </Trigger> 
         <Trigger Property="IsPressed" Value="True"> 
          <Setter Property="Background" Value="Red"/> 
          <Setter Property="BorderBrush" Value="Red"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
      </Setter>