Address as DependencyProperty?

Jul 7, 2010 at 10:13 AM
Hello, is there a simple way to implement the 'Address' property as a DependencyProperty in the WPF version? The current control can't be used with MVVM scenarios because 'Address' ist not bindable. Bye Stefan
Coordinator
Jul 18, 2010 at 1:19 PM

Hi Stefan,

I'm late, sorry for this - currently totally busy with my study thesis. Anyway I changed the 'Address' property to DependencyProperty type (watch the recent source code commits). Thanks for the advice.

Markus

Jul 18, 2010 at 2:33 PM

Thanks, and I wish you a good result on your study thesis.

Jul 19, 2010 at 6:34 PM

Hi Markus...

It seems, that the dependency property don't work with databinding. The initial value is set to control correct, but changes in the viewmodel are not updated to the control anymore and if I change the value in control, these changes are not updated to the viewmodel.

Here is some code to proof (each time you click the button the last number from IP changed, but UI don't reflect this):

<Window x:Class="IPv4ControlSample.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:IpControlsWpf="clr-namespace:IpControlsWpf"
        Title="MainWindow" Height="350" Width="525">
    <Canvas>
        <IpControlsWpf:IPv4TextBox Address="{Binding IP}" Canvas.Left="32" Canvas.Top="21" Width="138" />
        <IpControlsWpf:IPv4TextBox Address="{Binding IP}" Canvas.Left="32" Canvas.Top="49" Width="138" />
        
        <Button Canvas.Left="244" Canvas.Top="48" Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
    </Canvas>
</Window>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;

namespace IPv4ControlSample
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            var d = new DomainViewModel() { IP = IPAddress.Parse("192.168.100.254") };

            this.DataContext = d;
        }

        // changed the last ip number in actual viewmodel
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var r = new Random();
            var dc = (DomainViewModel)this.DataContext;

            dc.IP = IPAddress.Parse(string.Format(@"192.168.100.{0}", Convert.ToByte(r.Next(0, 255))));
        }
    }

    // simple viewmodel 
    internal class DomainViewModel :  INotifyPropertyChanged
    {
        public IPAddress IP
        {
            get { return _ip; }
            set
            {
                _ip = value;
                OnPropertyChanged("IP");
            }
        }
        private IPAddress _ip;

    #region INotifyPropertyChanged Implementation
        public event PropertyChangedEventHandler PropertyChanged;
        protected readonly Dictionary<string, PropertyChangedEventArgs> eventArgsCache = new Dictionary<string, PropertyChangedEventArgs>();
		
        protected void OnPropertyChanged(PropertyChangedEventArgs args)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, args);
        }
        protected void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventArgs args = default(PropertyChangedEventArgs);
            if (!eventArgsCache.ContainsKey(propertyName))
                eventArgsCache.Add(propertyName, new PropertyChangedEventArgs(propertyName));
            else
                args = eventArgsCache[propertyName];

            OnPropertyChanged(args);
        }
    #endregion // INotifyPropertyChanged Implementation
    }
}

Coordinator
Jul 28, 2010 at 4:28 PM

Hi again,

sorry again for being late - I was out of home/office for some days and there's currently no time for this project. I'll try to look on your code (thanks btw.) and to fix the problems as soon as I can but it can take a while since my timetable is completely filled atm.

Regards