c# - 使用自定义模板和触发器设置 Button 的 BorderBrush

标签 c# wpf xaml

我正在尝试更改自定义模板中按钮的 borderbrush 属性。使用此方法更改鼠标悬停时的背景效果很好,但是 BorderBrush?纳达。

这是我的代码:

<Style x:Key="ImageButtonStyle" TargetType="Button">
            <Setter Property="OverridesDefaultStyle" Value="True"/>
            <Setter Property="Margin" Value="2"/>
            <Setter Property="Background" Value="LightGray"/>
            <Setter Property="BorderBrush" Value="Blue"/>
            <Setter Property="BorderThickness" Value="3"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" Margin="1,0,0,-9">
                            <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,1,1,0" Height="94" Width="101"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="BorderBrush" Value="Red"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

编辑:实际上甚至连开头设置的蓝色边框都没有出现,所以这可能是相关的。

如果有任何重要性,这里是使用样式的地方。

<Button Margin="191,10,138,109" Style="{StaticResource ImageButtonStyle}">
    <Image/>
</Button>

最佳答案

您创建样式将 BorderThickness 设置为 3,但随后覆盖默认可视化树,因此不考虑此厚度。添加这个

 BorderThickness="{TemplateBinding BorderThickness}" 

为了看到它。

关于c# - 使用自定义模板和触发器设置 Button 的 BorderBrush,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25786714/

相关文章:

c# DataContractSerializer 问题

c# - WPF 中的 ValidationRule 与行为

xaml - 如何将对象从 xaml 页面传递到另一个页面?

c# - Entity Framework Core - 两个实体之间的多个一对多关系

wpf - 如何在MVVM中显示消息框

c# - MVC 和 jQuery 日期选择器

WPF - 如何在任何时候只扩展一个扩展器

c# - 事件冒泡和 RoutedEventArgs Source 属性

c# - Untitesting/Moq 方法调用 webrequest

c# - MongoDb Upsert死锁