几个关于控件的优先级: UseSystemPasswordChar > PasswordChar > 控件属性设置

本文介绍在C#中如何使用代码设置Windows窗体上密码框的显示字符,包括如何优先使用代码设置而非控件属性,并解释了不同设置间的优先级。
 1 using System;
 2 using System.Data;
 3 using System.Drawing;
 4 using System.Text;
 5 using System.Windows.Forms;
 6 namespace Test05
 7 {
 8     public partial class Form1 : Form
 9     {
10         public Form1()
11         {
12             InitializeComponent();
13         }
14         private void Form1_Load(object sender, EventArgs e)
15         {
16             textBox1.PasswordChar = '*';
17             textBox2.UseSystemPasswordChar = true;
18         }
19     }
20 }





textBox1.PasswordChar = '*',在窗体设置中选择的是“#”,而且不报错,需要我们以代码为主,代码中设置的显示符会最终决定显示效果,而且,优先级
UseSystemPasswordChar > PasswordChar > 控件属性设置

转载于:https://www.cnblogs.com/liuyaozhi/p/4955688.html

<Style x:Key="PasswordBoxStyle1" TargetType="{x:Type PasswordBox}"> <Setter Property="控件重写:PasswordBoxHelper.Attach" Value="True"/> <Setter Property="VerticalContentAlignment" Value="Center"/> <Setter Property="PasswordChar" Value="●"/> <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/> <Setter Property="BorderBrush" Value="{StaticResource TextBox.Static.Border}"/> <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/> <Setter Property="HorizontalContentAlignment" Value="Left"/> <Setter Property="FocusVisualStyle" Value="{x:Null}"/> <Setter Property="AllowDrop" Value="true"/> <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/> <Setter Property="Stylus.IsFlicksEnabled" Value="False"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type PasswordBox}"> <Border x:Name="border" CornerRadius="0" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> <!--重写构造PasswordBox--> <Grid x:Name="PART_InnerGrid"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <!--PasswordBox原有的显示节点--> <ScrollViewer x:Name="PART_ContentHost" BorderThickness="0" IsTabStop="False" VerticalAlignment="Stretch" Background="{x:Null}" VerticalContentAlignment="Center" Margin="3,6,0,0"/> <!--创建明文显示的TextBox--> <TextBox x:Name="PART_PasswordShower" FontWeight="{TemplateBinding FontWeight}" FontSize="{TemplateBinding FontSize}" VerticalContentAlignment="Center" BorderBrush="Transparent" Text="{Binding Path=(控件重写:PasswordBoxHelper.Password),RelativeSource={RelativeSource TemplatedParent}}" BorderThickness="0" Visibility="Collapsed" HorizontalAlignment="Stretch" VerticalAlignment="Center"/> <!--创建提示字符--> <TextBlock x:Name="PART_PlaceHolder" Text="{Binding Path=(控件重写:ControlAttachProperty.PlaceHolder),RelativeSource={RelativeSource TemplatedParent}}" Visibility="Collapsed" Opacity="0.6" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="5,5"/> <!--触发按钮显示样式--> <ToggleButton x:Name="PART_ToggleEye" Grid.Column="1" Width="35" BorderThickness="0" Margin="0,2,0,0"> <ToggleButton.Style> <Style TargetType="ToggleButton"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ToggleButton"> <Label Style="{StaticResource FontAwesome}" Content="{StaticResource Look}" x:Name="icon"/> <ControlTemplate.Triggers> <Trigger Property="IsChecked" Value="True"> <Setter TargetName="icon" Property="Content" Value="{StaticResource NotLook}"/> </Trigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> </ToggleButton.Style> </ToggleButton> </Grid> </Border> <ControlTemplate.Triggers> <Trigger Property="IsEnabled" Value="false"> <Setter Property="Opacity" TargetName="border" Value="0.56"/> </Trigger> <Trigger Property="IsMouseOver" Value="true"> <Setter Property="BorderBrush" TargetName="border" Value="Black"/> </Trigger> <Trigger Property="IsKeyboardFocused" Value="true"> <Setter Property="BorderBrush" TargetName="border" Value="Black"/> </Trigger> <!--密码框为空设置按钮禁用--> <Trigger Property="控件重写:PasswordBoxHelper.Password" Value=""> <Setter TargetName="PART_ToggleEye" Property="IsEnabled" Value="False"/> </Trigger> <!--按住按钮,更改按钮背景图片并设置明文框显示且密码框不显示且不占用--> <Trigger Property="IsChecked" SourceName="PART_ToggleEye" Value="true"> <!--<Setter TargetName="PART_ToggleEye" Property="Background"> <Setter.Value> <ImageBrush ImageSource="eye.png"/> </Setter.Value> </Setter>--> <Setter TargetName="PART_ContentHost" Property="Visibility" Value="Collapsed"/> <Setter TargetName="PART_PasswordShower" Property="Visibility" Value="Visible"/> <Setter TargetName="PART_PasswordShower" Property="Margin" Value="0,2,0,0"/> </Trigger> <!--密码框为空不且没有获取焦点时,设置提示文字显示--> <MultiTrigger> <MultiTrigger.Conditions> <Condition Property="控件重写:PasswordBoxHelper.Password" Value=""/> <Condition Property="IsFocused" Value="False"/> </MultiTrigger.Conditions> <Setter TargetName="PART_PlaceHolder" Property="Visibility" Value="Visible"/> </MultiTrigger> </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>这个怎么写成转为avalonia的控件
最新发布
08-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值