Question

Is it possible to use a model as a DependencyProperty of a custom control? I want to do this because I want to create a custom control which is basically a image drawer that gets a name and a list of datapoints for drawing.

Something like this:

Model:

public class Draw : NotificationObject
{

public Draw(string name, List<System.Drawing.PointF> data)
    {
        Name = name;
        Data = data;

    }

    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            if (_name != value)
            {
                _name = value;
                RaisePropertyChanged(() => Name);
            }
        }
    }

    private List<System.Drawing.PointF> _data;
    public List<System.Drawing.PointF> Data
    {
        get { return _data; }
        set
        {
            if (_data != value)
            {
                _data = value;
                RaisePropertyChanged(() => Data);
            }
        }
    }
}
}

Custom Control:

public class MyCanvas: System.Windows.Controls.Image
{
   static void itemsChangedCallBack(DependencyObject property,
   DependencyPropertyChangedEventArgs args)
    {
        MyCanvas searchTextBox = (MyCanvas)property;
        Console.WriteLine("got update");
        searchTextBox.Items = (Draw)args.NewValue;
    }

   public static readonly DependencyProperty ItemsProperty =
   DependencyProperty.Register("Items",
   typeof(Draw),
   typeof(MyCanvas),new PropertyMetadata(new PropertyChangedCallback(itemsChangedCallBack)));

    public Draw Items
    {
        get { return (Draw)GetValue(ItemsProperty); }
        set { SetValue(ItemsProperty, value); }
    }
}

And of course the XAML:

<myClass:MyCanvas x:Name="Canvas1" Items="{Binding drawModel,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" SizX="1600" SizY="200" />

And the ViewModel call:

public class MainWindowViewModel : BaseViewModel
   {

   public Draw drawModel { get; set; }

   public MainWindowViewModel()
       {
           drawModel = new Draw("first", null); // custom control is notified
       }

   private someFunction() //within another thread but should not matter
       {
           drawModel.Data = newData; // custom control should be notified but is not
       }
}

My problem is that if I do change the drawModel.Data (property) within the ViewModel I do not get a notification to the custom control. I did it before for a simple string instead of the model and it worked. It works the first time the drawModel gets initialized but not if I update the Data property later on.

Was it helpful?

Solution

It works the first time the drawModel gets initialized but not if I update the Data property later on.

You should make Data an ObservableCollection<PointF> (or, even better, ObservableCollection<System.Windows.Point>, as WPF's Point already supports floating point values).

The issue is that adding to, removing from, or changing a List<T> doesn't provide any form of notication to WPF that things have changed. ObservableCollection<T> implements INotifyCollectionChanged, which is the collection version of INotifyPropertyChagned.

Note that this will still not trigger an update within your control. If you require that, you could subscribe to the Draw.Data's CollectionChanged event to be notified of changes wtihin the collection.

OTHER TIPS

Your view model doesn't implement INotifiyPropertyChanged, For the first time your setting the property drawModel, in the constructor, which will be called before the UI is created completely and once UI is created, and when it is loaded it will load the current value of the property, but when you change the value again then you have to notify the UI that the value of particular property is changed.

If you have changed the values of any of the properties in the class Draw it could have been reflected since that implements INotifiyPropertyChanged, but your changing the value of drawModel which is in view-model and this doesn;t implement INotifiyPropertyChanged hence it doesn't update

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top