2017-08-30 212 views
0

如何停止我的ToggleButton在懸停或按下時改變其顏色?它不斷獲得Windows對這些事件的選擇顏色。我試過設置前景色和背景色,但沒有幫助。禁用切換按鈕選擇顏色

<ToggleButton 
    Canvas.Left="20" Canvas.Top="20" 
    Width="200" Height="150" 
    BorderBrush="Red" BorderThickness="5"> 
Click Me 
</ToggleButton> 

由我開始被定義的色彩應用後:

enter image description here

在鼠標點擊的顏色變成藍色:

enter image description here

+2

那些藍顏色的按鈕默認設定控件模板觸發器。你需要創建一個自定義模板 – ASh

回答

0

由於火山灰的提示我GOOGLE瞭如何更改按鈕的控件模板風格,它現在正常工作(該代碼添加對進入切換按鈕元素):

<ToggleButton.Style> 
    <Style TargetType="{x:Type ToggleButton}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ToggleButton"> 
        <Border BorderThickness="5" BorderBrush="Red" Background="Gray"> 
         <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/> 
        </Border> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
     <Style.Triggers> 
      <Trigger Property="IsChecked" Value="True"> 
       <Setter Property="Background" Value="White" /> 
      </Trigger> 
     </Style.Triggers> 
    </Style> 
</ToggleButton.Style> 
+1

是的,這就是我想到的,但 - 觸發屬性=「IsChecked」......不改變任何東西,可以刪除,邊框不是紅色5px在這裏 – ASh

0

修改的模板Button有點我刪除它的一些觸發器:

<Button Canvas.Left="20" Canvas.Top="20" 
     Width="200" Height="150" 
     BorderBrush="Red" BorderThickness="5" Content="Click Me"> 
    <Button.Template> 
     <ControlTemplate TargetType="{x:Type Button}"> 
      <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> 
       <ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
      </Border> 
      <ControlTemplate.Triggers> 
       <Trigger Property="IsDefaulted" Value="true"> 
        <Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/> 
       </Trigger> 
       <!--<Trigger Property="IsMouseOver" Value="true"> 
          <Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/> 
          <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/> 
         </Trigger> 
         <Trigger Property="IsPressed" Value="true"> 
          <Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/> 
          <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/> 
         </Trigger>--> 
       <Trigger Property="IsEnabled" Value="false"> 
        <Setter Property="Background" TargetName="border" Value="#FFF4F4F4"/> 
        <Setter Property="BorderBrush" TargetName="border" Value="#FFADB2B5"/> 
        <Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="#FF838383"/> 
       </Trigger> 
      </ControlTemplate.Triggers> 
     </ControlTemplate> 
    </Button.Template> 
</Button>