0

1234 にヒットしてから 3 回バックスペースを押すと、ユーザーに次の行を表示する textbox/IValueConverter を作成しようとしています。

1
*2
**3
***4
***
**
*

現時点での私の最大の問題は、IValueConverter が ViewModel に「***4」を保存しているため、データが失われていることです。

通常の PasswordBox を使用せずに、このような入力データをマスクするための一般的な戦略はありますか?

4

1 に答える 1

0

ダミーを作成し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;
    }
}

結果:

ここに画像の説明を入力

于 2013-02-05T00:41:32.867 に答える