【WPF】TextBox样式重写注意事项

1、普通控件重写需要添加一行

 <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"  Margin="{TemplateBinding Padding}"></ContentPresenter>

2、TextBox之类的编辑控件重写,需要填加一行

 <ScrollViewer x:Name="PART_ContentHost"></ScrollViewer>

只有这样才能进入编辑模式

举个栗子:

<ControlTemplate x:Key="RoundTextBoxTemplate" TargetType="TextBox">
        <Border CornerRadius="5" BorderBrush="LightSkyBlue" BorderThickness="2" Background="{TemplateBinding Background}" Margin="3">
            <ScrollViewer x:Name="PART_ContentHost"></ScrollViewer>
        </Border>
    </ControlTemplate>

    <Style x:Key="RoundTextBoxStyle" TargetType="TextBox">
        <Setter Property="Template" Value="{DynamicResource ResourceKey=RoundTextBoxTemplate}" ></Setter>
    </Style>

上面就是实现最简单的TextBox重写样式的功能了

 下面记录一个圆角文本框外带提示信息的功能:2021-06-07

<TextBox MinWidth="100" MinHeight="30">
            <TextBox.Resources>
                <VisualBrush x:Key="HintText" TileMode="None" Opacity="0.5" Stretch="None" AlignmentX="Left">
                    <VisualBrush.Visual>
                        <TextBlock FontStyle="Italic" Text="请输入文本"/>
                    </VisualBrush.Visual>
                </VisualBrush>
            </TextBox.Resources>
            <TextBox.Style>
                <Style TargetType="TextBox">
                    <Style.Setters>
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate>
                                    <Border x:Name="border" CornerRadius="5" BorderThickness="2" BorderBrush="Red">
                                        <TextBox Name="txt" VerticalAlignment="Center" BorderThickness="0" BorderBrush="{x:Null}" Background="{x:Null}"></TextBox>
                                    </Border>
                                    <ControlTemplate.Triggers>
                                        <Trigger SourceName="txt" Property="Text" Value="{x:Null}">
                                            <Setter TargetName="border" Property="Background" Value="{StaticResource HintText}"/>
                                        </Trigger>
                                        <Trigger  SourceName="txt" Property="Text" Value="">
                                            <Setter TargetName="border" Property="Background" Value="{StaticResource HintText}"/>
                                        </Trigger>
                                    </ControlTemplate.Triggers>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style.Setters>
                </Style>
            </TextBox.Style>

        </TextBox>

这样也可以实现:

 <TextBox Width="200" Height="30" VerticalContentAlignment="Center">
            <TextBox.Resources>
                <VisualBrush x:Key="HintText" TileMode="None" Opacity="0.5" Stretch="None" AlignmentX="Left">
                    <VisualBrush.Visual>
                        <TextBlock FontStyle="Italic" Text="提示文字"/>
                    </VisualBrush.Visual>
                </VisualBrush>
                <Style TargetType="Border">
                    <Setter Property="CornerRadius" Value="5"></Setter>
                    <Setter Property="BorderThickness" Value="2"></Setter>
                    <Setter Property="BorderBrush" Value="Red"></Setter>
                </Style>
            </TextBox.Resources>
            <TextBox.Style>
                <Style TargetType="TextBox">
                    <Style.Triggers>
                        <Trigger Property="Text" Value="{x:Null}">
                            <Setter Property="Background" Value="{StaticResource HintText}"/>
                        </Trigger>
                        <Trigger Property="Text" Value="">
                            <Setter Property="Background" Value="{StaticResource HintText}"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </TextBox.Style>
        </TextBox>

 

posted on 2016-11-02 19:14  梦琪小生  阅读(1328)  评论(0编辑  收藏  举报

导航