2012-02-23 3 views
1

Silverlight 4, MVVM 및 PRISM 4에서 사용자 지정 텍스트 상자를 만들었습니다. 사용자 지정 텍스트 상자에 동적 동작 링크가있어 동적으로 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}"/> 

(내가 바인드 정적 텍스트 모드를 나는 경우)은 다음과 같은 내 뷰 모델 코드

[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

+0

다음 오류 만 제공합니다. Set 'QSys.Library.Controls.PasswordTextBox.TextMode'속성이 예외를 throw했습니다. [줄 : 40 위치 : 144] – imdadhusen

+1

예외에는 항상 메시지가 있습니다. 바인딩은 일반적인 속성에는 적용 할 수 없다고 생각합니다. TextMode를 DependencyProperty로 변경해야합니다. – chopikadze

답변

1

시도는 PasswordTextBox 클래스에

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에 대한 링크를 제공하지만 기본적으로 실버 라이트를 위해 당신의 예외가 무엇입니까 같은

+0

귀하의 노력에 감사 드리며, 나는 신속하게 알려 드리겠습니다. – imdadhusen

+0

매우 우수! 나는 당신의 도움으로 해결책을 얻었습니다. 내 +1의 투표 – imdadhusen