禁用ToggleButton选择颜色

时间:2017-08-30 15:23:38

标签: wpf xaml

当我悬停或按下时,如何阻止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 个答案:

答案 0 :(得分:0)

稍微修改<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> 的模板,从中删除一些触发器:

c = [1,2]
# do something magical
print(c with magical stuff) 
>>>[(1), (2), (1, 1), (1, 2), (2, 1), (2, 2)]

答案 1 :(得分:0)

感谢ASh的提示,我用Google搜索了如何更改按钮的controltemplate样式,现在工作正常(此代码添加到ToggleButton元素中):

<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>