2013-02-05 29 views
0

我想创建一个文本框/ IValueConverter,如果用户点击1234,然后退格3次,将向用户显示以下行。用IValueConverter屏蔽文本框输入而不丢失数据

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

我最大的那一刻问题是,我失去的数据作为的IValueConverter在我的ViewModel节约“*** 4”。

是否有任何常见的策略来掩盖像这样的输入数据而不使用普通的PasswordBox?

+0

使用这项工作的正确控制。 [PasswordBox](http://msdn.microsoft.com/en-us/library/system.windows.controls.passwordbox(v = vs.110).aspx) – Will

回答

0

您可以创建一个虚拟的TextBlockLabel来显示蒙版,并将TextBox文本颜色设置为透明。这样实际的数据被保存到模型中,并且*只显示在标签中。

喜欢的东西(很粗糙的例子)

<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> 

转换器(pleae忽略可怕的代码,它只是一个演示:))

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; 
    } 
} 

结果:

enter image description here

+0

谢谢sa_ddam213,这是我的第一个方法,但因为它不使用文本框内部的ContentPresenter没有显示carot。 –

+0

插入符号将以此方法显示,因为我们只将forground设置为Transparent而不是插入符号笔刷。看到我的图像更新。我没有覆盖我刚刚覆盖顶部的一个标签的文本框的样式。 –