2012-02-23 63 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