2012-02-23 71 views
1

我已创建自定义的文本框4.自定义文本框具有动态行为链接它动态地设置的TextMode要么密码或文本。错误在Silverlight 4中,MVVM和PRISM绑定自定义文本框属性

这是工作完美。 (如果我绑定的TextMode静)以下

<control:PasswordTextBox x:Name="customTextBox2" Width="100" Height="30" Grid.Row="4" Grid.Column="1" Text="{Binding Email}" TextMode="Password"/> 

这是给我的错误(如果我与动态绑定)

<control:PasswordTextBox x:Name="customTextBox1" Width="100" Height="30" Grid.Row="4" Grid.Column="1" Text="{Binding Email}" TextMode="{Binding WritingMode}"/> 

是下面是我的ViewModel代码

[Export] 
    [PartCreationPolicy(CreationPolicy.NonShared)] 
    public class UserRightsViewModel : NotificationObject, IRegionMemberLifetime 
    { 
private Mode _writingMode = Mode.Text; 
public Mode WritingMode 
     { 
      get { return _writingMode; } 
      set 
      { 
       _writingMode = value; RaisePropertyChanged("WritingMode"); 
      } 
     } 

[ImportingConstructor] 
     public UserRightsViewModel(IEventAggregator eventAggregator, IRegionManager regionManager) 
     { 
UserSecurity security = new UserSecurity(); 
      FormSecurity formSecurity = security.GetSecurityList("Admin"); 
formSecurity.WritingMode = Mode.Password; 
} 
} 

enum

namespace QSys.Library.Enums 
{ 
    public enum Mode 
    { 
     Text, 
     Password 
    } 
} 

以下代码用于自定义PasswordTextBox

namespace QSys.Library.Controls 
{ 
    public partial class PasswordTextBox : TextBox 
    { 
     #region Variables 
     private string _Text = string.Empty; 
     private string _PasswordChar = "*"; 
     private Mode _TextMode = Mode.Text; 
     #endregion 

     #region Properties 
     /// <summary> 
     /// The text associated with the control. 
     /// </summary> 
     public new string Text 
     { 
      get { return _Text; } 
      set 
      { 
       _Text = value; 
       DisplayMaskedCharacters(); 
      } 
     } 
     /// <summary> 
     /// Indicates the character to display for password input. 
     /// </summary> 
     public string PasswordChar 
     { 
      get { return _PasswordChar; } 
      set { _PasswordChar = value; } 
     } 
     /// <summary> 
     /// Indicates the input text mode to display for either text or password. 
     /// </summary> 
     public Mode TextMode 
     { 
      get { return _TextMode; } 
      set { _TextMode = value; } 
     } 
     #endregion 

     #region Constructors 
     public PasswordTextBox() 
     { 
      this.TextChanged += new TextChangedEventHandler(PasswordTextBox_TextChanged); 
      this.KeyDown += new System.Windows.Input.KeyEventHandler(PasswordTextBox_KeyDown); 
      this.Loaded += new RoutedEventHandler(PasswordTextBox_Loaded); 
     } 
     #endregion 

     #region Event Handlers 
     void PasswordTextBox_Loaded(object sender, System.Windows.RoutedEventArgs e) 
     { 
      //this.TextChanged += ImmediateTextBox_TextChanged; 
     } 
     public void PasswordTextBox_TextChanged(object sender, TextChangedEventArgs e) 
     { 
      if (base.Text.Length >= _Text.Length) _Text += base.Text.Substring(_Text.Length); 
      DisplayMaskedCharacters(); 
     } 
     public void PasswordTextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e) 
     { 
      int cursorPosition = this.SelectionStart; 
      int selectionLength = this.SelectionLength; 
      // Handle Delete and Backspace Keys Appropriately 
      if (e.Key == System.Windows.Input.Key.Back || e.Key == System.Windows.Input.Key.Delete) 
      { 
       if (cursorPosition < _Text.Length) 
        _Text = _Text.Remove(cursorPosition, (selectionLength > 0 ? selectionLength : 1)); 
      } 
      base.Text = _Text; 
      this.Select((cursorPosition > _Text.Length ? _Text.Length : cursorPosition), 0); 
      DisplayMaskedCharacters(); 
     } 
     #endregion 

     #region Private Methods 
     private void DisplayMaskedCharacters() 
     { 
      int cursorPosition = this.SelectionStart; 
      // This changes the Text property of the base TextBox class to display all Asterisks in the control 
      base.Text = new string(_PasswordChar.ToCharArray()[0], _Text.Length); 
      this.Select((cursorPosition > _Text.Length ? _Text.Length : cursorPosition), 0); 
     } 
     #endregion 

     #region Public Methods 
     #endregion 
    } 
} 

如果我与动态绑定,则出现跟随错误。

设置属性'QSys.Library.Controls.PasswordTextBox.TextMode'引发异常。 [行:40位置:144]

你的回答将不胜感激。 在此先感谢。 Imdadhusen

+0

你是什么异常?信息? – chopikadze 2012-02-23 09:44:12

+0

它只是给以下错误:设置属性“QSys.Library.Controls.PasswordTextBox.TextMode”引发了异常。 [行:40的位置:144] – imdadhusen 2012-02-23 10:06:38

+1

异常总是有消息。我认为绑定不适用于平常的财产。你应该改变你的TextMode到DependencyProperty – chopikadze 2012-02-23 12:55:54

回答

1

尝试在您的密码框类改变

public Mode TextMode 
{ 
    get { return _TextMode; } 
    set { _TextMode = value; } 
} 

public static readonly DependencyProperty TextModeProperty = 
      DependencyProperty.Register("TextMode", typeof(Mode), typeof(PasswordTextBox), new PropertyMetadata(default(Mode))); 

public Mode TextMode 
{ 
    get { return (Mode) GetValue(TextModeProperty); } 
    set { SetValue(TextModeProperty, value); } 
} 

你可以在这里阅读更多:

从第二个链接的主要段落:

A DependencyProperty supports the following capabilities in Windows Presentation Foundation (WPF):

....

  • The property can be set through data binding. For more information about data binding dependency properties, see How to: Bind the Properties of Two Controls.

我提供链接,WPF,但基本上为Silverlight这是相同的

+0

感谢您的努力,我会尽快通知您。 – imdadhusen 2012-02-23 14:21:43

+0

非常好!我得到了你的帮助。我的投票+1 – imdadhusen 2012-02-24 05:41:56

相关问题