DevFear
Topic Author
Posts: 53
Joined: 29 Jun 2022, 12:36

Inheritance from Dependencyobject

06 Oct 2023, 12:07

To work, I decided to create a primitive DependencyObject, which is a property of the control. However, Noesis writes that this object should be thread-free.
public abstract  class ValueHandler : DependencyObject
	{
		public abstract Type ValueType { get; }

		public ValueHandler()
		{
		}

		public abstract bool Handle(string oldText, string newText, out string resultText);
	}
Error:
ArgumentException: Dependency property 'SmartTwin.UI.Controls.TextBoxWrapper.ValueHandler' default value must be free threaded
Noesis.DependencyProperty.ValidateDefaultValue (System.String name, System.Type type, System.Type owner, System.Object value) (at
 
DevFear
Topic Author
Posts: 53
Joined: 29 Jun 2022, 12:36

Re: Inheritance from Dependencyobject

06 Oct 2023, 12:16

So I made a mistake and realized that the class needs to be inherited from Freezable so that it can be used in defaultValue when registering in DependencyProperty. Is there any practice showing when it is better to use DependencyObject and when Freezable?
 
User avatar
sfernandez
Site Admin
Posts: 3000
Joined: 22 Dec 2011, 19:20

Re: Inheritance from Dependencyobject

06 Oct 2023, 19:11

You can define a DependencyProperty of a type inheriting from DependencyObject, but in that case the default value should be null. Otherwise that DependencyObject instance set as default value could be accessed from different threads.

If you want to set a non-null default value you should create a Freezable inherited object and Freeze() it.
 
DevFear
Topic Author
Posts: 53
Joined: 29 Jun 2022, 12:36

Re: Inheritance from Dependencyobject

07 Oct 2023, 09:10

Thanks!

Who is online

Users browsing this forum: Bing [Bot], hcpizzi, sfernandez and 0 guests