绑定自定义文本框属性时出错

发布于 2025-01-08 07:52:29 字数 4880 浏览 5 评论 0原文

我在 Silverlight 4、MVVM 和 PRISM 4 中创建了自定义文本框。自定义文本框具有动态行为链接,可以将 TextMode 动态设置为密码或文本。

这是完美的。 (如果我绑定 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;
}
}

枚举

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]

您的回答将不胜感激。 提前致谢。 伊姆达胡森

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}"/>

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

following is the enum

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

叫思念不要吵 2025-01-15 07:52:30

尝试将您的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); }
}

您可以在此处阅读更多信息:

DependencyProperty 第二个链接的主要段落是:

DependencyProperty 支持 Windows 中的以下功能
演示基础 (WPF):

...

  • 该属性可以通过数据绑定来设置。有关数据绑定依赖属性的详细信息,请参阅如何:绑定
    两个控件的属性。

我提供了 WPF 的链接,但基本上 Silverlight 是一样的

Try to change in your PasswordTextBox class

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

to

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

You can read more here:

The main paragraph from the second link is:

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.

I provide links for WPF, but basically for Silverlight it's the same

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文