【问题标题】:Modify a button's background and border within a UserControl在 UserControl 中修改按钮的背景和边框
【发布时间】:2012-11-14 01:42:48
【问题描述】:

我有一个定制的用户控件,里面有一个按钮。我有各种使用此控件的 XAML,但在其中一个 XAML 中,我希望按钮上有一个图像。这很好用,但我似乎无法让按钮的边框消失。我可以设置背景颜色、边框粗细和其他各种属性,但无论我做什么我都无法摆脱边框。我已经查看了有关如何使用样式覆盖模板的各种 SO 主题,但这似乎也对我没有帮助。以下是我尝试过的最后一段sn-p。

<Style TargetType="{x:Type CustomButton}" x:Key="btnnoborder">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CustomButton}">
                <Border Background="Transparent">
                    <ContentPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

编辑

我附上了我在测试这些 sn-ps 时看到的小图片

(常用按钮)

(使用 Viktor La Croix 的 XAML 后的按钮)

【问题讨论】:

  • 尝试了但无济于事,甚至尝试了其中一个 cmets 链接到的一个班轮。那只是抛出了一个异常。
  • 在上述XAML中,将BorderBorderBrush属性设置为Transparent,将BorderThickness属性设置为0
  • 那么使用后我的样式边框不见了?这真的很奇怪。接下来您唯一需要做的就是使用 ContentPresenter 的属性。最简单...删除所有这些图像是好的。

标签: wpf button user-controls border


【解决方案1】:

我不打算回答这个问题,但是你运行了吗?你确定它不仅在设计时吗?我用了你的风格,效果很好。运行时没有边框。在设计时,我看到按钮本身的边框和该按钮内的图像。

我的自定义样式:

    <UserControl.Resources>
    <Style x:Key="ButtonStyle2" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid>
                        <Rectangle/>
                        <ContentPresenter HorizontalAlignment="Left" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Margin="11,7,0,9"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsFocused" Value="True"/>
                        <Trigger Property="IsDefaulted" Value="True"/>
                        <Trigger Property="IsMouseOver" Value="True"/>
                        <Trigger Property="IsPressed" Value="True"/>
                        <Trigger Property="IsEnabled" Value="False"/>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</UserControl.Resources>

    <Button Style="{StaticResource ButtonStyle2}" Height="30" HorizontalAlignment="Left" Margin="782,211,0,0" Name="button5" VerticalAlignment="Top" Width="129">
        <Image Source="image.png"/>
    </Button>

使用您的自定义样式,它看起来像这样:

但这只是在设计时。在运行时没有边界。

编辑

如果有任何边框刷。它不在那个按钮样式中。

【讨论】:

  • 我确实在运行时看到它以及使用我上面提到的代码。我看到您在 UserControl 本身中有您的代码。这是否需要,因为我在调用按钮的窗口的 XAML 中执行此操作。我认为这样只有窗口需要修改按钮的需要。
  • 我尝试使用您的 sn-p 并编辑了我最初的问题以显示它给我的输出。
  • @Seb 我修改了我的样式,让它看起来和你的一模一样。我删除了 ControlTemplate 中 ContentPresenter 中设置的所有属性并删除了触发器……这些都是多余的,没有任何用处。还有一件事...我的 UserControl 是您的窗口,无论您将样式放在哪里都没有关系...当您只需要在该窗口中时,然后将其放入 ...
  • 我让它运行起来了。我尝试在实例化控件的位置嵌入资源,并且效果很好。感谢您的帮助。
【解决方案2】:

为了改进我发布的评论,请将 BorderBrushBorderThickness 属性设置为 TemplateBinding 值:

<Style TargetType="{x:Type CustomButton}" x:Key="btnnoborder">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type CustomButton}">
                <Border Background="Transparent" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                    <ContentPresenter/>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这将允许您的按钮从声明它的实例继承值:

<local:CustomButton BorderBrush="Transparent" BorderThickness="0" Content="Click Me!"/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-20
    • 2011-12-30
    • 2018-11-03
    • 2016-04-28
    • 2011-12-07
    • 1970-01-01
    • 2017-03-06
    • 2022-01-18
    相关资源
    最近更新 更多