Question

i'm trying to create a user control that acts as an IP address holder. Generally the control is composed of 4 TextBoxes that together has the full IP address. in the user control code behind there is a public property that holds the IP address of type IPAddress. I have been trying to expose this property so i could bind a property from my ViewModel to it.

here is the property from the user control i want to expose:

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

its value gets updated correctly but i can't get the value into my ViewModel variable Using Binding. i know i need to use a dependency property in some way, but i don't know how to tie its value with my property.

thanks ahead :)

Was it helpful?

Solution

Its easier than that, you just need to use a MaskedInputTextEdit such as this one, http://wpftoolkit.codeplex.com/wikipage?title=MaskedTextBox

or pick one of these. Where can I find a free masked TextBox in WPF?

Textbox validation for IP Address in WPF

OTHER TIPS

well i found the solution, the problem was that my VM didn't update correctly, apparently i had to add a specific metadata to my user control's DP that says it binds in TwoWay mode. as follows:

        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");
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top