@@ -97,6 +97,9 @@ public TextBox() : base()
97
97
98
98
// TextBox only accepts plain text, so change TextEditor's default to that.
99
99
this . TextEditor . AcceptsRichContent = false ;
100
+
101
+ SetValue ( TemplateButtonCommandProperty , new RelayCommand < string > ( OnTemplateButtonClick ) ) ;
102
+
100
103
}
101
104
102
105
#endregion Constructors
@@ -962,7 +965,228 @@ public Typography Typography
962
965
return new Typography ( this ) ;
963
966
}
964
967
}
968
+
969
+ /// <summary>
970
+ /// Property for <see cref="Icon"/>.
971
+ /// </summary>
972
+ public static readonly DependencyProperty IconProperty = DependencyProperty . Register (
973
+ nameof ( Icon ) ,
974
+ typeof ( IconElement ) ,
975
+ typeof ( TextBox ) ,
976
+ new PropertyMetadata ( null , null , IconSourceElementConverter . ConvertToIconElement )
977
+ ) ;
978
+
979
+ /// <summary>
980
+ /// Property for <see cref="IconPlacement"/>.
981
+ /// </summary>
982
+ public static readonly DependencyProperty IconPlacementProperty = DependencyProperty . Register (
983
+ nameof ( IconPlacement ) ,
984
+ typeof ( ElementPlacement ) ,
985
+ typeof ( TextBox ) ,
986
+ new PropertyMetadata ( ElementPlacement . Left )
987
+ ) ;
988
+
989
+ /// <summary>
990
+ /// Property for <see cref="PlaceholderText"/>.
991
+ /// </summary>
992
+ public static readonly DependencyProperty PlaceholderTextProperty = DependencyProperty . Register (
993
+ nameof ( PlaceholderText ) ,
994
+ typeof ( string ) ,
995
+ typeof ( TextBox ) ,
996
+ new PropertyMetadata ( String . Empty )
997
+ ) ;
998
+
999
+ /// <summary>
1000
+ /// Property for <see cref="PlaceholderEnabled"/>.
1001
+ /// </summary>
1002
+ public static readonly DependencyProperty PlaceholderEnabledProperty = DependencyProperty . Register (
1003
+ nameof ( PlaceholderEnabled ) ,
1004
+ typeof ( bool ) ,
1005
+ typeof ( TextBox ) ,
1006
+ new PropertyMetadata ( true )
1007
+ ) ;
1008
+
1009
+ /// <summary>
1010
+ /// Property for <see cref="ClearButtonEnabled"/>.
1011
+ /// </summary>
1012
+ public static readonly DependencyProperty ClearButtonEnabledProperty = DependencyProperty . Register (
1013
+ nameof ( ClearButtonEnabled ) ,
1014
+ typeof ( bool ) ,
1015
+ typeof ( TextBox ) ,
1016
+ new PropertyMetadata ( true )
1017
+ ) ;
965
1018
1019
+ /// <summary>
1020
+ /// Property for <see cref="ShowClearButton"/>.
1021
+ /// </summary>
1022
+ public static readonly DependencyProperty ShowClearButtonProperty = DependencyProperty . Register (
1023
+ nameof ( ShowClearButton ) ,
1024
+ typeof ( bool ) ,
1025
+ typeof ( TextBox ) ,
1026
+ new PropertyMetadata ( false )
1027
+ ) ;
1028
+
1029
+ /// <summary>
1030
+ /// Property for <see cref="IsTextSelectionEnabledProperty"/>.
1031
+ /// </summary>
1032
+ public static readonly DependencyProperty IsTextSelectionEnabledProperty = DependencyProperty . Register (
1033
+ nameof ( IsTextSelectionEnabled ) ,
1034
+ typeof ( bool ) ,
1035
+ typeof ( TextBox ) ,
1036
+ new PropertyMetadata ( false )
1037
+ ) ;
1038
+
1039
+ /// <summary>
1040
+ /// Property for <see cref="TemplateButtonCommand"/>.
1041
+ /// </summary>
1042
+ public static readonly DependencyProperty TemplateButtonCommandProperty = DependencyProperty . Register (
1043
+ nameof ( TemplateButtonCommand ) ,
1044
+ typeof ( IRelayCommand ) ,
1045
+ typeof ( TextBox ) ,
1046
+ new PropertyMetadata ( null )
1047
+ ) ;
1048
+
1049
+ #region Properties
1050
+
1051
+ /// <summary>
1052
+ /// Gets or sets displayed <see cref="IconElement"/>.
1053
+ /// </summary>
1054
+ public IconElement Icon
1055
+ {
1056
+ get => ( IconElement ) GetValue ( IconProperty ) ;
1057
+ set => SetValue ( IconProperty , value ) ;
1058
+ }
1059
+
1060
+ /// <summary>
1061
+ /// Defines which side the icon should be placed on.
1062
+ /// </summary>
1063
+ public ElementPlacement IconPlacement
1064
+ {
1065
+ get => ( ElementPlacement ) GetValue ( IconPlacementProperty ) ;
1066
+ set => SetValue ( IconPlacementProperty , value ) ;
1067
+ }
1068
+
1069
+ /// <summary>
1070
+ /// Gets or sets numbers pattern.
1071
+ /// </summary>
1072
+ public string PlaceholderText
1073
+ {
1074
+ get => ( string ) GetValue ( PlaceholderTextProperty ) ;
1075
+ set => SetValue ( PlaceholderTextProperty , value ) ;
1076
+ }
1077
+
1078
+ /// <summary>
1079
+ /// Gets or sets a value determining whether to display the placeholder.
1080
+ /// </summary>
1081
+ public bool PlaceholderEnabled
1082
+ {
1083
+ get => ( bool ) GetValue ( PlaceholderEnabledProperty ) ;
1084
+ set => SetValue ( PlaceholderEnabledProperty , value ) ;
1085
+ }
1086
+
1087
+ /// <summary>
1088
+ /// Gets or sets a value determining whether to enable the clear button.
1089
+ /// </summary>
1090
+ public bool ClearButtonEnabled
1091
+ {
1092
+ get => ( bool ) GetValue ( ClearButtonEnabledProperty ) ;
1093
+ set => SetValue ( ClearButtonEnabledProperty , value ) ;
1094
+ }
1095
+
1096
+ /// <summary>
1097
+ /// Gets or sets a value determining whether to show the clear button when <see cref="TextBox"/> is focused.
1098
+ /// </summary>
1099
+ public bool ShowClearButton
1100
+ {
1101
+ get => ( bool ) GetValue ( ShowClearButtonProperty ) ;
1102
+ protected set => SetValue ( ShowClearButtonProperty , value ) ;
1103
+ }
1104
+
1105
+ /// <summary>
1106
+ /// TODO
1107
+ /// </summary>
1108
+ public bool IsTextSelectionEnabled
1109
+ {
1110
+ get => ( bool ) GetValue ( IsTextSelectionEnabledProperty ) ;
1111
+ set => SetValue ( IsTextSelectionEnabledProperty , value ) ;
1112
+ }
1113
+
1114
+ /// <summary>
1115
+ /// Command triggered after clicking the button.
1116
+ /// </summary>
1117
+ public IRelayCommand TemplateButtonCommand => ( IRelayCommand ) GetValue ( TemplateButtonCommandProperty ) ;
1118
+
1119
+ #endregion
1120
+
1121
+
1122
+
1123
+ /// <inheritdoc />
1124
+ protected override void OnTextChanged ( TextChangedEventArgs e )
1125
+ {
1126
+ base . OnTextChanged ( e ) ;
1127
+
1128
+ if ( PlaceholderEnabled && Text . Length > 0 )
1129
+ PlaceholderEnabled = false ;
1130
+
1131
+ if ( ! PlaceholderEnabled && Text . Length < 1 )
1132
+ PlaceholderEnabled = true ;
1133
+
1134
+ RevealClearButton ( ) ;
1135
+ }
1136
+
1137
+ /// <inheritdoc />
1138
+ protected override void OnGotFocus ( RoutedEventArgs e )
1139
+ {
1140
+ base . OnGotFocus ( e ) ;
1141
+
1142
+ CaretIndex = Text . Length ;
1143
+
1144
+ RevealClearButton ( ) ;
1145
+ }
1146
+
1147
+ /// <inheritdoc />
1148
+ protected override void OnLostFocus ( RoutedEventArgs e )
1149
+ {
1150
+ base . OnLostFocus ( e ) ;
1151
+
1152
+ HideClearButton ( ) ;
1153
+ }
1154
+
1155
+ /// <summary>
1156
+ /// Reveals the clear button by <see cref="ShowClearButton"/> property.
1157
+ /// </summary>
1158
+ protected void RevealClearButton ( )
1159
+ {
1160
+ if ( ClearButtonEnabled && IsKeyboardFocusWithin )
1161
+ ShowClearButton = Text . Length > 0 ;
1162
+ }
1163
+
1164
+ /// <summary>
1165
+ /// Hides the clear button by <see cref="ShowClearButton"/> property.
1166
+ /// </summary>
1167
+ protected void HideClearButton ( )
1168
+ {
1169
+ if ( ClearButtonEnabled && ! IsKeyboardFocusWithin && ShowClearButton )
1170
+ ShowClearButton = false ;
1171
+ }
1172
+
1173
+ /// <summary>
1174
+ /// Triggered when the user clicks the clear text button.
1175
+ /// </summary>
1176
+ protected virtual void OnClearButtonClick ( )
1177
+ {
1178
+ if ( Text . Length > 0 )
1179
+ Text = string . Empty ;
1180
+ }
1181
+
1182
+ /// <summary>
1183
+ /// Triggered by clicking a button in the control template.
1184
+ /// </summary>
1185
+ protected virtual void OnTemplateButtonClick ( string parameter )
1186
+ {
1187
+
1188
+ OnClearButtonClick ( ) ;
1189
+ }
966
1190
#endregion Public Properties
967
1191
968
1192
//------------------------------------------------------
0 commit comments