绑定自定义文本框属性时出错 [英] Error in Binding Custom Textbox Property

查看:106
本文介绍了绑定自定义文本框属性时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Silverlight 4,MVVM和PRISM 4中创建了自定义文本框.该自定义文本框具有动态行为,可将其动态地将TextMode设置为Password或Text.
这是完美的工作. (如果我将TextMode绑定为静态)

I have created custom Textbox in Silverlight 4, MVVM and PRISM 4. The custom text box has dynamic behavior link it dynamically set TextMode to either Password or Text.
This is working perfect. ( if i am bind TextMode static)

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



这给我一个错误(如果我与动态绑定)



This is giving me an error (if i am binding with dynamic)

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



以下是我的ViewModel代码



following is my ViewModel code

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

下面是枚举

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


以下自定义PasswordTextBox的代码


following code for Custom 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''引发异常. [Line:40 Position:144]

您的回答将不胜感激.
提前致谢.
Imdadhusen


I am getting following error if i am binding with dynamically.
Set property ''QSys.Library.Controls.PasswordTextBox.TextMode'' threw an exception. [Line: 40 Position: 144]

Your answer would be appreciated.
Thanks in advance.
Imdadhusen

推荐答案

大家好,
我使用以下代码解决了此问题,我更改了PasswordTextBox类的TextMode属性.
Hi All,
I resolved this issue using following code, I have changed PasswordTextBox class TextMode property.
public Mode TextMode
{
    get { return _TextMode; }
    set { _TextMode = value; }
}




To

public Mode TextMode
{
    get { return (Mode) GetValue(TextModeProperty); }
    set { SetValue(TextModeProperty, value); }
}
public static readonly DependencyProperty TextModeProperty =
DependencyProperty.Register(&quot;TextMode&quot;, typeof(Mode), typeof(PasswordTextBox), new PropertyMetadata(default(Mode)));



特别感谢chopikadze帮助我解决了这个问题.
http://stackoverflow.com/questions/9410431/error-in-binding-custom-文本框属性 [ ^ ]

感谢所有寻找并尝试帮助我的人.
Imdadhusen



Specially thanks to chopikadze for helping me to fix this.
http://stackoverflow.com/questions/9410431/error-in-binding-custom-textbox-property[^]

Thanks all of you guys who searching and trying to help me.
Imdadhusen


这篇关于绑定自定义文本框属性时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆