我是WPF的初学者。所以,我需要了解边界和内容之间的差距。我也在 Buttons 中遇到了这个问题。
简而言之,我有一个MenuItem的样式,如下所示,问题是当我将项目悬停在菜单项边缘和内容之间移动指针时,边框将消失。我认为填充或边距有问题。
我希望当指针位于Item内时,边框始终可见。
<Style TargetType="MenuItem" x:Key="MenuItemsStyle">
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="3,0,3,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="MenuItem">
<Border Margin="{TemplateBinding Margin}"
x:Name="B1"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}">
<Grid VerticalAlignment="Center">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<ContentPresenter
x:Name="Textb"
Grid.Column="1"
Content="{TemplateBinding Header}"
ContentStringFormat="{TemplateBinding HeaderStringFormat}"
ContentSource="Header"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
TextElement.Foreground="Black">
<ContentPresenter.ContentTemplate>
<DataTemplate>
<Border>
<Grid>
<TextBlock Text="{TemplateBinding Property=Content}"/>
</Grid>
</Border>
</DataTemplate>
</ContentPresenter.ContentTemplate>
</ContentPresenter>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsHighlighted" Value="True">
</Trigger>
<Trigger Property="IsEnabled" Value="False">
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsEnabled" Value="True"/>
<Condition Property="IsHighlighted" Value="True"/>
</MultiTrigger.Conditions>
<Setter Property="BorderBrush" TargetName="B1" Value="Gray"/>
</MultiTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
答案 0 :(得分:1)
您应该设置最外层Background
的{{1}}属性。
Border