【问题标题】:Change from Decorator to Control从装饰者到控制者的转变
【发布时间】:2011-10-07 08:35:42
【问题描述】:

很抱歉提出新问题,但对于我的旧问题,我找到了解决方案。我想要一个 bindablePasswordBox 并找到 this 示例。现在我正在尝试将此代码集成到我的项目中。它像我想要的那样工作,但还有一个问题。因为我想将 BindablePasswordBox 的样式基于其他控件,所以我必须将其从示例中的装饰器更改为此处代码中的控件:

    public class BindablePasswordBox : Control
    {
        public static readonly DependencyProperty PasswordProperty;

        private bool isPreventCallback;
        private RoutedEventHandler savedCallback;

        static BindablePasswordBox()
        {
            PasswordProperty = DependencyProperty.Register(
                "Password",
                typeof(string),
                typeof(BindablePasswordBox),
                new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, new PropertyChangedCallback(OnPasswordPropertyChanged))
            );
        }

        public BindablePasswordBox()
        {
            savedCallback = HandlePasswordChanged;

            PasswordBox passwordBox = new PasswordBox();
            passwordBox.PasswordChanged += savedCallback;
            **Child** = passwordBox;
        }

        public string Password
        {
            get { return GetValue(PasswordProperty) as string; }
            set { SetValue(PasswordProperty, value); }
        }

        private static void OnPasswordPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs eventArgs)
        {
            BindablePasswordBox customPasswordBox = (BindablePasswordBox)d;
            PasswordBox passwordBox = (PasswordBox)customPasswordBox.**Child**;

            if (customPasswordBox.isPreventCallback)
            {
                return;
            }

            passwordBox.PasswordChanged -= customPasswordBox.savedCallback;
            passwordBox.Password = (eventArgs.NewValue != null) ? eventArgs.NewValue.ToString() : "";
            passwordBox.PasswordChanged += customPasswordBox.savedCallback;
        }

        private void HandlePasswordChanged(object sender, RoutedEventArgs eventArgs)
        {
            PasswordBox passwordBox = (PasswordBox)sender;

            isPreventCallback = true;
            Password = passwordBox.Password;
            isPreventCallback = false;
        }
    }
}

我得到了两个 Visual Studio 下划线的错误。我用粗体字标记了这两个位置。两个“孩子”都被标记为错误。

请帮助我并给我一个提示来解决这个问题。我是 wpf 的新手,所以请帮助我。

赛波

【问题讨论】:

  • Control 没有Child。它有Content
  • 您的意思是,我可以将其更改为 Child by Content 吗?那行不通:(
  • 对不起,弄错了。您可以将基类从 Control 更改为 ContentControl,以便 Content 可用。

标签: wpf controls decorator


【解决方案1】:

Child 仅定义为Decorator 的一部分。

【讨论】:

  • 你知道解决方法吗,所以 bindablePasswordBox 做同样的事情,但只是一个控件而不是装饰器?
猜你喜欢
  • 1970-01-01
  • 2013-03-03
  • 2011-06-13
  • 2021-07-21
  • 2018-04-18
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多