加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 当画笔用作背景时,如何从WPF重复按钮中删除边框和效果

发布时间:2020-12-15 21:35:42 所属栏目:百科 来源:网络整理
导读:我正在使用 WPF中的repeatbutton对象,该按钮需要基于图形(图像画笔背景,没有边框或其他多余的效果). 我遇到的问题是,无论我尝试什么,按钮周围仍然有一个白色矩形,而且,我不确定现在看了几个小时之后,我确信我理解为什么或如何删除它. 我在XAML中使用以下内容
我正在使用 WPF中的repeatbutton对象,该按钮需要基于图形(图像画笔背景,没有边框或其他多余的效果).

我遇到的问题是,无论我尝试什么,按钮周围仍然有一个白色矩形,而且,我不确定现在看了几个小时之后,我确信我理解为什么或如何删除它.

我在XAML中使用以下内容创建了按钮

<RepeatButton Content="" BorderThickness="0" HorizontalAlignment="Left" Margin="334,265,0" VerticalAlignment="Top" Width="149" Height="100" Background="{DynamicResource ImageBrush_Decrement}" Style="{DynamicResource RepeatButtonStyle_noflash}" d:LayoutOverrides="HorizontalAlignment"/>

你会注意到我引用了一个“RepeatButtonNoFlash”样式,我用它来删除defaultrender(因为它引起了闪烁效果,这种样式在共享字典中定义如下

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
    <Style x:Key="ButtonFocusVisual">
        <Setter Property="Control.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="0" StrokeDashArray="1 2"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <!-- Resource dictionary entries should be defined here. -->

    <Style x:Key="BaseButtonStyle" TargetType="{x:Type ButtonBase}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ButtonBase}">
                    <Microsoft_Windows_Themes:ButtonChrome x:Name="Chrome" Background="{TemplateBinding Background}" RenderMouSEOver="False" RenderPressed="{TemplateBinding IsPressed}" RenderDefaulted="False" SnapsToDevicePixels="true" Style="{DynamicResource ButtonChromeStyle_noborder}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Microsoft_Windows_Themes:ButtonChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsKeyboardFocused" Value="true">
                            <Setter Property="RenderDefaulted" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="ToggleButton.IsChecked" Value="true">
                            <Setter Property="RenderPressed" TargetName="Chrome" Value="true"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="RepeatButtonStyle_noflash" BasedOn="{StaticResource BaseButtonStyle}" TargetType="{x:Type RepeatButton}">
        <Setter Property="Stylus.IsPressAndHoldEnabled" Value="false"/>
    </Style>
</ResourceDictionary>

问候

解决方法

它来自Microsoft_Windows_Themes:ButtonChrome尝试用边框或其他东西替换它,应该摆脱它.

<Style x:Key="BaseButtonStyle" TargetType="{x:Type ButtonBase}">
        <Setter Property="FocusVisualStyle" Value="{StaticResource ButtonFocusVisual}"/>
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="BorderBrush" Value="Transparent"/>
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ButtonBase}">
                    <Border Background="{TemplateBinding Background}">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="#ADADAD"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读