Wpf IP地址文本框用户控件

Wpf IP地址文本框用户控件,wpf,binding,user-controls,textbox,ip-address,Wpf,Binding,User Controls,Textbox,Ip Address,我正在尝试创建一个充当IP地址持有者的用户控件。 通常,控件由4个文本框组成,这些文本框一起具有完整的IP地址。 在后面的用户控制代码中有一个公共属性,它保存类型为IPAddress的IP地址。 我一直在尝试公开此属性,以便可以将ViewModel中的属性绑定到它 以下是我要公开的用户控件的属性: public IPAddress IPAddressObject { get { return new IPAddress(m_IPAd

我正在尝试创建一个充当IP地址持有者的用户控件。 通常,控件由4个文本框组成,这些文本框一起具有完整的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变量中。 我知道我需要以某种方式使用依赖属性,但我不知道如何将它的值与我的属性联系起来


谢谢:)

比这更简单,你只需要使用一个MaskedInputTextEdit,比如这个

或者选择其中一个


我找到了解决方案,问题是我的虚拟机没有正确更新,显然我必须向我的用户控件的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");
        }
    }

我必须说,我不知道如何使用该控件,我在扩展工具包中找到了它,但一个示例会很好。获取它并返回一个字符串,然后使用IPAddress.Parse()