按钮上没有显示边框

本文关键字:显示 边框 按钮 | 更新日期: 2023-09-27 18:07:59

我是WPF的新手,可能太简单了。我不明白为什么BorderBrush没有显示在移除Button上。默认情况下,Button是不可见的,但当鼠标在Button上时显示。当鼠标在Button上时,底层的TextBlock显示为应有的,但没有Border

有没有人有一个建议,我如何查看按钮的Border .

以下是我的XAML:
<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="150" Width="325">
<Window.Resources>
    <Storyboard x:Key="MakeToolbarVisible">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
            <EasingDoubleKeyFrame KeyTime="0:0:0.2" Value="0.5"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Storyboard x:Key="MakeToobarHidden">
        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)">
            <EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
        </DoubleAnimationUsingKeyFrames>
    </Storyboard>
    <Style x:Key="Toolbar">
        <Style.Triggers>
            <Trigger Property="UIElement.IsMouseOver" Value="True">
                <Trigger.ExitActions>
                    <BeginStoryboard Storyboard="{StaticResource MakeToobarHidden}"/>
                </Trigger.ExitActions>
                <Trigger.EnterActions>
                    <BeginStoryboard Storyboard="{StaticResource MakeToolbarVisible}"/>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <Border>
        <Grid Margin="0">
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition Height="30"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition Width="30" />
            </Grid.ColumnDefinitions>
            <TextBlock Text="enter some text here!" Margin="8,19.058,8,0" VerticalAlignment="Top" Foreground="#B43C1C26" HorizontalAlignment="Right"/>
            <TextBox TextWrapping="WrapWithOverflow" Margin="8,19.058,0,2.463" FontSize="11" AcceptsReturn="True" AcceptsTab="True" Visibility="Visible" BorderThickness="0" Background="{x:Null}"/>
            <WrapPanel Grid.Row="1" Margin="8,8,8,8">
                <TextBlock Text="29.8.1995" Foreground="#B43C1C26" FontSize="10" />
            </WrapPanel>
            <StackPanel Grid.Column="1" Margin="5,5,5,8" Grid.RowSpan="2" Opacity="0" Style="{StaticResource Toolbar}">
                <Button x:Name="Remove" BorderBrush="DarkRed" BorderThickness="1" Cursor="Hand">
                    <Button.Template>
                        <ControlTemplate>
                            <TextBlock Text="r" FontFamily="Marlett" FontSize="12"/>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
            </StackPanel>
        </Grid>
    </Border>
</Grid>

按钮上没有显示边框

通过设置Buttons ControlTemplate,你可以将Buttons的默认模板替换为一个没有边框的模板。

要显示边框,将其添加到ControlTemplate中,如下所示:

        <StackPanel Grid.Column="1" Margin="5,5,5,8" Grid.RowSpan="2" Opacity="0" Style="{StaticResource Toolbar}">
            <Button x:Name="Remove" BorderBrush="DarkRed" BorderThickness="1" Cursor="Hand">
                <Button.Template>
                    <ControlTemplate>
                        <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
                             <TextBlock Text="r" FontFamily="Marlett" FontSize="12"/>
                        </Border>
                    </ControlTemplate>
                </Button.Template>
            </Button>
        </StackPanel>

通过TemplateBinding可以绑定到模板化控件的属性,在本例中是Button的属性。

如果你想让按钮看起来像一个普通的按钮,你不应该重新设计它只需像这样设置你想要更改的属性:

  <StackPanel Grid.Column="1" Margin="5,5,5,8" Grid.RowSpan="2" Opacity="0" Style="{StaticResource Toolbar}">
     <Button x:Name="Remove" BorderBrush="DarkRed" BorderThickness="1" Cursor="Hand"
                            FontSize="12" FontFamily="Marlett" Content="r"/>
   </StackPanel>

您已经覆盖了ButtonControlTemplate,因此您将失去默认的按钮模板。

根据你的代码,我可以看到的是你想设置FontFamilyFontSize的文本在你的按钮,你可以实现不覆盖模板像这样-

       <StackPanel Grid.Column="1" Margin="5,5,5,8" Grid.RowSpan="2" Opacity="0"
                   Style="{StaticResource Toolbar}">
            <Button x:Name="Remove" BorderBrush="DarkRed"
                    BorderThickness="1" Cursor="Hand" Content="r"
                    FontFamily="Marlett" FontSize="12"/>
        </StackPanel>