6

如何在 XAML 或 C# 代码中创建以下两个文本框(不是矩形)

带边框的文本框

4

2 回答 2

11

您可以使用矩形创建“破折号”样式

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TextBlock Width="200" Height="40" Name="Textblock1" Text="Hello World!" TextAlignment="Center" FontSize="20"/>
        <Rectangle Width="{Binding ElementName=Textblock1, Path=ActualWidth}" Height="{Binding ElementName=Textblock1, Path=ActualHeight}" StrokeDashArray="0.0 6.0 0.0" Stroke="Black" StrokeThickness="2"  />
    </Grid>
</Window>

在此处输入图像描述

对于双线,您可能会创建 2 个边框

<Window x:Class="WpfApplication5.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Border BorderBrush="Black" Width="200" Height="40" BorderThickness="1">
            <Border BorderBrush="Black" Margin="2" BorderThickness="1">
                <TextBlock  Name="Textblock1" Text="Hello World!" TextAlignment="Center" FontSize="20"/>
            </Border>
        </Border>
    </Grid>
</Window>

在此处输入图像描述

于 2012-11-28T00:44:38.403 回答
4

您应该使用 Blend 来编辑文本框的控件模板。我这样做是为了你的双重身份。

 <Style x:Key="DashedTextBoxStyle" BasedOn="{x:Null}" TargetType="{x:Type TextBox}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="Padding" Value="1"/>
        <Setter Property="AllowDrop" Value="true"/>
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
        <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBox}">
                    <Border BorderBrush="Black" BorderThickness=".5">
                         <Border BorderBrush="Black" Margin="1" BorderThickness=".5">
                                <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                    </Border>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </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>

并将其应用于文本框之类的。

<TextBox Text="Hello world" Width="100" Height="30.667" Canvas.Left="150" Canvas.Top="90" Style="{DynamicResource DashedTextBoxStyle}"/>

您可能需要对其进行自定义以获得更好的外观。我还从样式中删除了默认边框。

你可以为你的虚线做同样的事情。

希望能帮助到你。

于 2012-11-28T03:30:40.687 回答