Wpf 文本块问题的圆形边框

Wpf 文本块问题的圆形边框,wpf,xaml,Wpf,Xaml,我试着给我的文本块一个圆形的边框。根据我的研究,我应该在边框标记中嵌入一个textblock,然后设置边框的角半径。对我来说,它的效果是在整行周围加上一个边框。我做错了什么?我将边框的颜色设置为蓝色,以显示正在发生的事情。理想情况下,我会将其更改为与textblock背景相同的颜色,以使其具有无缝圆角 <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"

我试着给我的文本块一个圆形的边框。根据我的研究,我应该在边框标记中嵌入一个textblock,然后设置边框的角半径。对我来说,它的效果是在整行周围加上一个边框。我做错了什么?我将边框的颜色设置为蓝色,以显示正在发生的事情。理想情况下,我会将其更改为与textblock背景相同的颜色,以使其具有无缝圆角

<Grid>                
   <Grid.RowDefinitions>
       <RowDefinition Height="Auto"></RowDefinition>                   
   </Grid.RowDefinitions>


   <Border Grid.Row="0" Margin="25" Grid.Column="1" Background="Blue"
           BorderThickness="1" BorderBrush="Red"  CornerRadius="30">
           <TextBlock  Margin="50" Padding="200,0,200,0"
                       FontSize="100"
                       FontWeight="Bold"
                       Background="Black"
                       Foreground="White"    
                       VerticalAlignment="Center"
                       Text="bla bla bla"
                       HorizontalAlignment="Center">
           </TextBlock>
    </Border>
</Grid>

查看栅格和边框的
水平对齐
/
垂直对齐
值。设置最适合您的需求的内容


像这样的工具非常适合在不必构建整个应用程序的情况下处理此类问题。

查看网格和边框的
水平对齐
/
垂直对齐
值。设置最适合您的需求的内容


像这样的工具非常适合在不必构建整个应用程序的情况下处理此类问题。

更好的方法是为TextBox创建默认样式的副本,并修改ControlTemplate中的边框。下面是一个简单的应用程序,它提取并修改了默认的文本框样式,使其成为圆角

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/>
        <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/>
        <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5"/>
        <Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
            <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="AllowDrop" Value="true"/>
            <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" CornerRadius="5">
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
                        <Condition Property="IsSelectionActive" Value="false"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBox Margin="10" Style="{DynamicResource TextBoxStyle1}" Text="bla bla bla"></TextBox>
        </StackPanel>
    </Grid>
</Window>


如果希望此样式应用于应用程序中的所有文本框,则应将该样式移动到资源字典中,并从样式定义中删除x:Key=“TextBoxStyle1”。默认情况下,它将应用于所有TextBox,并且不需要为每个TextBox设置样式。

更好的方法是为TextBox创建默认样式的副本,并修改ControlTemplate中的边框。下面是一个简单的应用程序,它提取并修改了默认的文本框样式,使其成为圆角

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/>
        <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/>
        <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5"/>
        <Style x:Key="TextBoxStyle1" TargetType="{x:Type TextBox}">
            <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
            <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/>
            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="AllowDrop" Value="true"/>
            <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True" CornerRadius="5">
                            <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Opacity" TargetName="border" Value="0.56"/>
                            </Trigger>
                            <Trigger Property="IsMouseOver" Value="true">
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.MouseOver.Border}"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="true">
                                <Setter Property="BorderBrush" TargetName="border" Value="{StaticResource TextBox.Focus.Border}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsInactiveSelectionHighlightEnabled" Value="true"/>
                        <Condition Property="IsSelectionActive" Value="false"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="SelectionBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <StackPanel>
            <TextBox Margin="10" Style="{DynamicResource TextBoxStyle1}" Text="bla bla bla"></TextBox>
        </StackPanel>
    </Grid>
</Window>


如果希望此样式应用于应用程序中的所有文本框,则应将该样式移动到资源字典中,并从样式定义中删除x:Key=“TextBoxStyle1”。默认情况下,它将应用于所有文本框,并且不需要为每个文本框设置样式。

尝试设置边框元素的
水平对齐
属性…尝试设置边框元素的
水平对齐
属性。。。