2016-07-31 109 views
1

我有一個按鈕,其中有一個撤銷圖像。 我想讓按鈕在用戶單擊(按下)按鈕時顯示邊框。按鈕IsPressed觸發器不工作

<Button Margin="0,7,12,0" HorizontalAlignment="Right" Height="20" Width="23"> 
        <Button.Style> 
         <Style TargetType="{x:Type Button}"> 
          <Setter Property="Background" Value="White"/> 
          <Setter Property="Template"> 
           <Setter.Value> 
            <ControlTemplate TargetType="{x:Type Button}"> 
             <Border Background="{TemplateBinding Background}"> 
              <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
             </Border> 
            </ControlTemplate> 
           </Setter.Value> 
          </Setter> 
          <Style.Triggers> 
           <Trigger Property="IsPressed" Value="True"> 
            <Setter Property="BorderThickness" Value="1"/> 
           </Trigger> 
           <Trigger Property="IsPressed" Value="False"> 
            <Setter Property="BorderThickness" Value="0"/> 
           </Trigger> 
          </Style.Triggers> 
         </Style> 
        </Button.Style> 
        <Image Grid.Column="0" Grid.Row="0" Margin="0,0,0,0" Height="20" Width="20" Source="/Images/Undo.png"> 
         <Image.Style> 
          <Style TargetType="Image"> 
           <Style.Triggers> 
            <Trigger Property="IsMouseOver" Value="True"> 
             <Setter Property="Opacity" Value="0.7"/> 
            </Trigger> 
           </Style.Triggers> 
          </Style> 
         </Image.Style> 
        </Image> 
       </Button> 

但它沒有工作。 任何幫助將不勝感激!

+0

@jstreet能否請您重新發布您的解決方案。我非常抱歉,無法標記。謝謝 – Merv

回答

0

我對您的樣式做了以下更改:1)在ControlTemplate內移動了觸發器; 2)將x:Name分配給Border,因此可以在觸發器中引用; 3)定義爲BorderBrush

<Button Margin="0" HorizontalAlignment="Center" Height="25" Width="75" VerticalAlignment="Center"> 
     <Button.Style> 
      <Style TargetType="{x:Type Button}"> 
       <Setter Property="Background" Value="White"/> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="{x:Type Button}"> 
          <Border x:Name="b0" Background="{TemplateBinding Background}"> 
           <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
          </Border> 
          <ControlTemplate.Triggers> 
           <Trigger Property="IsPressed" Value="True"> 
            <Setter Property="BorderThickness" TargetName="b0" Value="1"/> 
            <Setter Property="BorderBrush" TargetName="b0" Value="Blue"/> 
           </Trigger> 
          </ControlTemplate.Triggers> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </Button.Style> 
     <Image Grid.Column="0" Grid.Row="0" Margin="0,0,0,0" Height="20" Width="20" Source="/Images/Undo.jpg"> 
      <Image.Style> 
       <Style TargetType="Image"> 
        <Style.Triggers> 
         <Trigger Property="IsMouseOver" Value="True"> 
          <Setter Property="Opacity" Value="0.7"/> 
         </Trigger> 
        </Style.Triggers> 
       </Style> 
      </Image.Style> 
     </Image> 
    </Button> 

enter image description here

0

它不起作用,因爲按鈕模板直接將邊框厚度設置爲1,忽略按鈕元素本身的邊框厚度屬性。您正在成功更改未使用的值。

如果要更改邊框厚度,則必須複製和修改按鈕模板。