关于文本块问题的圆形边框

时间:2018-10-17 15:06:17

标签: wpf xaml

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

<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>

2 个答案:

答案 0 :(得分:2)

查看Grid和Border的<%= f.input :tag, as: :select, collection: Tag.all, input_html: {class: 'chosen-select', :data => {:placeholder => "your custom placeholder"}, multiple: true} %> / HorizontalAlignment值。设置最适合您要求的内容。

诸如Kaxaml之类的工具非常适合解决此类问题,而无需构建整个应用程序。

答案 1 :(得分:1)

一种更好的方法是为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设置样式。