[英]How do I change the MaxLength of the PasswordBox
I am trying to be able to change the MaxLength of the Password box based on another control.我试图能够基于另一个控件更改密码框的 MaxLength。 how do I change the passwordBox MaxLength when user clicks the toggleButton.当用户单击切换按钮时,如何更改密码框 MaxLength。
Below is the code I have for the toggleBox and PasswordBox but this is only allowing 1 as the maxLength when toggled.下面是我为 toggleBox 和 PasswordBox 提供的代码,但这仅允许 1 作为切换时的 maxLength。
<PasswordBox MaxLength="{Binding ElementName=toggleUseToken, Path=IsChecked, Mode=OneWay}" x:Name="textboxPassword" BorderThickness="0" ToolTip="Enter your password" Password="password" Style="{StaticResource textboxpassword}" Grid.Column="1"/>
<ToggleButton x:Name="toggleUseToken" Grid.Column="3" ToolTip="Remember Me" Style="{StaticResource toggleToken}" Height="20"/>
You can do something like this:你可以这样做:
<StackPanel>
<PasswordBox MaxLength="{Binding ElementName=ToggleButton, Path=IsChecked, Converter={StaticResource ToggleButtonToMaxLengthConverterKey}}"/>
<ToggleButton x:Name="ToggleButton" Content="Set Length"/>
</StackPanel>
And use Converter:并使用转换器:
public class ToggleButtonToMaxLengthConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool isChecked)
{
return isChecked ? 5 : 12;
}
return 12;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
You could a Style
with a DataTrigger
to set the MaxLength
property:您可以使用带有DataTrigger
的Style
来设置MaxLength
属性:
<PasswordBox x:Name="textboxPassword" BorderThickness="0" ToolTip="Enter your password" Password="password" Grid.Column="1">
<PasswordBox.Style>
<Style TargetType="PasswordBox" BasedOn="{StaticResource textboxpassword}">
<Setter Property="MaxLength" Value="12" />
<Style.Triggers>
<DataTrigger Binding="{Binding IsChecked, ElementName=toggleUseToken}" Value="True">
<Setter Property="MaxLength" Value="5" />
</DataTrigger>
</Style.Triggers>
</Style>
</PasswordBox.Style>
</PasswordBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.