【问题标题】:IP address textbox user controlIP 地址文本框用户控件
【发布时间】:2014-04-06 06:46:01
【问题描述】:

我正在尝试创建一个充当 IP 地址持有者的用户控件。 通常,该控件由 4 个 TextBoxes 组成,它们一起具有完整的 IP 地址。 在后面的用户控制代码中有一个公共属性,它保存 IPAddress 类型的 IP 地址。 我一直在尝试公开此属性,以便可以将 ViewModel 中的属性绑定到它。

这是我要公开的用户控件的属性:

public IPAddress IPAddressObject
    {
        get
        {
            return new IPAddress(m_IPAddress);
        }
        set
        {
            m_IPAddress = value.GetAddressBytes();
            NotifyPropertyChanged("Octet1");
            NotifyPropertyChanged("Octet2");
            NotifyPropertyChanged("Octet3");
            NotifyPropertyChanged("Octet4");
        }
    }

它的值得到正确更新,但我无法使用绑定将值放入我的 ViewModel 变量中。 我知道我需要以某种方式使用依赖属性,但我不知道如何将其值与我的属性联系起来。

谢谢你:)

【问题讨论】:

    标签: wpf binding user-controls textbox ip-address


    【解决方案1】:

    比这更简单,你只需要使用一个 MaskedInputTextEdit,比如这个,http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox

    或选择其中之一。 Where can I find a free masked TextBox in WPF?

    Textbox validation for IP Address in WPF

    【讨论】:

    • 我必须说我不知道​​如何使用该控件,我在扩展工具包中找到了它,但一个例子会很好。
    • 获取它返回一个字符串然后使用 IPAddress.Parse() msdn.microsoft.com/en-us/library/…
    【解决方案2】:

    我找到了解决方案,问题是我的虚拟机没有正确更新,显然我必须向我的用户控件的 DP 添加一个特定的元数据,说明它以双向模式绑定。 如下:

            public static readonly DependencyProperty MyCustomProperty =DependencyProperty.Register("MyCustom", typeof(IPAddress), typeof(IPAddressTextBox), new FrameworkPropertyMetadata(IPAddress.Parse("0.0.0.0"), FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
        public IPAddress MyCustom
        {
            get
            {
                return this.GetValue(MyCustomProperty) as IPAddress;
            }
            set
            {
                this.SetValue(MyCustomProperty, value);
               // NotifyPropertyChanged("MyCustom");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2016-12-02
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-01
      • 2015-06-21
      • 2012-09-01
      • 2014-09-27
      相关资源
      最近更新 更多