6

我在网上搜索带圆角的 TextBox 并找到如下的 xaml 代码:

 <Style TargetType="{x:Type my1:CustomTextBox}">
            <Setter Property="HorizontalContentAlignment" Value="Center"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate >
                        <Border Background="{TemplateBinding Background}" x:Name="Bd" 
BorderThickness="2" CornerRadius="5" BorderBrush="#FFF9EAB6">
                            ***<ScrollViewer x:Name="PART_ContentHost" />***
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="Bd" Property="BorderBrush" Value="#FFC7B0B0"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="True">
                                <Setter TargetName="Bd" Property="BorderBrush" Value="#FFC7B0B0"/>
                                <Setter Property="Foreground" Value="Black"/>
                            </Trigger>
                            <Trigger Property="IsKeyboardFocused" Value="False">
                                <Setter Property="Foreground" Value="#FFC7B0B0"/>
                            </Trigger>
                            <Trigger Property="Width" Value="Auto">
                                <Setter Property="MinWidth" Value="120"/>
                            </Trigger>
                            <Trigger Property="Height" Value="Auto">
                                <Setter Property="MinHeight" Value="27"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>

                </Setter.Value>
            </Setter>
        </Style>

我想知道什么是

<ScrollViewer x:Name="PART_ContentHost" />

详细说明,如果从中删除此行,为什么不能正确使用我的模板,请详细告诉我。

多谢。

4

4 回答 4

6

名为“PART_ContentHost”的部分包含控制核心,这是文本框本身,除了装饰。后面的文本框代码会寻找它,所以如果你重命名删除,控件将无法工作。在这种情况下,内容是可滚动的(因为文本框可以水平和垂直滚动文本)。

于 2012-05-09T04:14:29.533 回答
6

使用 xaml deign 的这一部分:

 <TextBox x:Name="usernameText" Height="30" Width="300"  TextWrapping="Wrap" Text="" FontSize="20" HorizontalContentAlignment="Center" LostFocus="usernameText_LostFocus">
                        <TextBox.Resources>
                            <Style TargetType="{x:Type Border}">
                                <Setter Property="CornerRadius" Value="10"/>
                            </Style>
                        </TextBox.Resources>
                    </TextBox>

形状会是这样的

于 2017-02-23T07:39:28.440 回答
5

如果您需要一个带圆角的简单文本框,您可以这样做:

<Border Padding="5" CornerRadius="5" BorderThickness="1" BorderBrush="LightGray" SnapsToDevicePixels="True" Background="White">
    <TextBox Background="Transparent" BorderThickness="0">This is beautifull ;)</TextBox>
</Border>
于 2013-02-13T15:15:49.510 回答
1

包含控件的ScrollViewer实际内容。您的控件不是真正的文本框,而是围绕 ScrollViewer 的边框(带有圆角),您需要在其中放置文本。如果不需要滚动,可以将 ScrollViewer 替换为文本框,即:

改变

<ScrollViewer x:Name="PART_ContentHost" />

<TextBox x:Name="PART_ContentHost" />
于 2012-05-09T04:19:52.497 回答