1234 にヒットしてから 3 回バックスペースを押すと、ユーザーに次の行を表示する textbox/IValueConverter を作成しようとしています。
1
*2
**3
***4
***
**
*
現時点での私の最大の問題は、IValueConverter が ViewModel に「***4」を保存しているため、データが失われていることです。
通常の PasswordBox を使用せずに、このような入力データをマスクするための一般的な戦略はありますか?
ダミーを作成しTextBlock
たりLabel
、マスクを表示したり、TextBox
テキスト coloe を透明に設定したりできます。このようにして、実際のデータがモデルに保存*
され、ラベルにのみ表示されます。
のようなもの(非常に大まかな例)
<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication13"
Title="MainWindow" Height="350" Width="525" Name="UI">
<Window.Resources>
<local:TextToStarConverter x:Key="TextToStarConverter" />
</Window.Resources>
<StackPanel>
<Grid>
<TextBox x:Name="txtbox" Foreground="Transparent" Text="{Binding MyModelProperty}" />
<Label Content="{Binding ElementName=txtbox, Path=Text, Converter={StaticResource TextToStarConverter}}" IsHitTestVisible="False" />
</Grid>
</StackPanel>
</Window>
コンバーター (恐ろしいコードは無視してください。これは単なるデモです:))
public class TextToStarConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string && !string.IsNullOrEmpty(value.ToString()))
{
return new string('*', value.ToString().Length -1) + value.ToString().Last().ToString();
}
return string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return null;
}
}
結果: