zoukankan      html  css  js  c++  java
  • WPF 实现带标题的TextBox

    这篇博客将分享在WPF中如何创建一个带Title的TextBox。首先请看一下最终的效果,

    实现思路:使用TextBlock+TextBox来实现,TextBlock用来显示Title。

    实现代码,

    TitleTextBox

        [TemplatePart(Name = TitleTextBlockKey, Type = typeof(TextBlock))]
        public class TitleTextBox : TextBox
        {
            private const string TitleTextBlockKey = "PART_TitleTextBlock";
    
            private TextBlock _tbkTitle;
    
            public static readonly DependencyProperty TitleProperty;
    
            public string Title
            {
                get { return (string)GetValue(TitleProperty); }
                set { SetValue(TitleProperty, value); }
            }
    
            static TitleTextBox()
            {
                DefaultStyleKeyProperty.OverrideMetadata(typeof(TitleTextBox), new FrameworkPropertyMetadata(typeof(TitleTextBox)));
    
                TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(TitleTextBox), new UIPropertyMetadata(new PropertyChangedCallback(TitlePropertyChangedCallback)));
            }
    
            private static void TitlePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                TitleTextBox ttb = d as TitleTextBox;
    
                if (ttb._tbkTitle != null)
                {
                    ttb._tbkTitle.Text = (string)e.NewValue;
                }
            }
    
            public override void OnApplyTemplate()
            {
                base.OnApplyTemplate();
    
                _tbkTitle = Template.FindName(TitleTextBlockKey, this) as TextBlock;
                _tbkTitle.Text = Title;
            }
        }

    定义TitleTextBox样式,

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="clr-namespace:WPFTitleTextBox.Resources.Styles"
                        xmlns:uc="clr-namespace:WPFTitleTextBox">
        <Style TargetType="{x:Type uc:TitleTextBox}">
            <Setter Property="SnapsToDevicePixels" Value="True"/>
            <Setter Property="FontSize" Value="12"/>
            <Setter Property="Height" Value="28"/>
            <Setter Property="UndoLimit" Value="0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type uc:TitleTextBox}">
                        <Border x:Name="OuterBorder" BorderBrush="#8b99bc" BorderThickness="1" CornerRadius="1" Background="#f7f7f7">
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="Auto"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
    
                                <TextBlock x:Name="PART_TitleTextBlock" Text="{Binding Title}" Foreground="#a7abb0" VerticalAlignment="Center" Margin="5,0"/>
                                <Line Grid.Column="1" Stroke="#ccd1d7" StrokeDashArray="2,2" StrokeThickness="1.5" X1="0" Y1="0" 
                                      X2="0" Y2="{TemplateBinding Height}" Margin="0,4"/>
    
                                <Border Grid.Column="2" Background="White">
                                    <ScrollViewer x:Name="PART_ContentHost" Margin="5,0" VerticalAlignment="Center" FontSize="14"/>
                                </Border>
                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>

    在XAML中需要引用TitleTextBox。

        <Grid>
            <StackPanel>
                <local:TitleTextBox Title="姓名" Width="270" Margin="5,10"/>
                <local:TitleTextBox Title="班级" Width="270"/>
                <local:TitleTextBox Title="专业" Width="270" Margin="5,10"/>
            </StackPanel>
        </Grid>

    使用时设置一下Title即可。使用方式和普通TextBox一样。

    以后如果遇到带Title的ComboBox,CheckBox等都可以参考上面的实现思路。

    感谢您的阅读,代码点击这里下载。

  • 相关阅读:
    记某app内购破解 – 安卓逆向菜鸟的初体验
    初探Android逆向:通过游戏APP破解引发的安全思考
    用IKVMC将jar转成dll供c#调用
    Java与.net 关于URL Encode 的区别
    RSA加密、解密、签名、验签的原理及方法
    C#使用SHA1加密类(RSAFromPkcs8)支持1024位和2048位私钥
    java与.net平台之间进行RSA加密验证
    RSA密钥,JAVA与.NET之间转换
    全面解决.Net与Java互通时的RSA加解密问题,使用PEM格式的密钥文件
    Android中Activity的启动模式(LaunchMode)和使用场景
  • 原文地址:https://www.cnblogs.com/yang-fei/p/4719196.html
Copyright © 2011-2022 走看看