Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constraint to limit the scope of an attached dependency property

Tags:

wpf

Is there a way to add a constraint to an attached dependency property so that it only can be applied to a specific type, something in the metadata?

If not, is it meaningful to explicit type the static Get-and Set-methods of the attached DPs?

Example:

If I have for example the following declaration:

public static int GetAttachedInt(DependencyObject obj) {
    return (int)obj.GetValue(AttachedIntProperty);
}

public static void SetAttachedInt(DependencyObject obj, int value) {
    obj.SetValue(AttachedIntProperty, value);
}

public static readonly DependencyProperty AttachedIntProperty = 
   DependencyProperty.RegisterAttached("AttachedInt", typeof(int), 
   typeof(Ownerclass), new UIPropertyMetadata(0));

would it be meaningfull to change it as follows, to only apply it to TextBoxes?

public static int GetAttachedInt(TextBox textBox) {
    return (int)textBox.GetValue(AttachedIntProperty);
}

public static void SetAttachedInt(TextBox textBox, int value) {
    textBox.SetValue(AttachedIntProperty, value);
}

public static readonly DependencyProperty AttachedIntProperty = 
   DependencyProperty.RegisterAttached("AttachedInt", typeof(int), 
   typeof(Ownerclass), new UIPropertyMetadata(0));

My question is, because this leads to an inconsistence, because GetValue and SetValue could be used anymore for any type and also in markup is no possibility to limit the attachement.

What I did formerly was that I added an exception into the PropertyChanged handler and raised there an exception that only the types xy are allowed.

What do you think?

like image 227
HCL Avatar asked Jul 19 '10 09:07

HCL


People also ask

What is difference between dependency property and attached property?

Attached properties allows container to create a property which can be used by any child UI elements whereas dependency property is associated with that particular elements and can help in notification of changes and reacting to that changes.

How do you set the value of a dependency property?

The XAML processor sets the value of the dependency property using the SetValue method: Setters not run on Dependency Properties? If you want to do something whenever the property is being set to a new value, you should register a callback: public static DependencyProperty TestProperty = DependencyProperty.

What are the advantages of dependency properties?

Advantages of a Dependency Property The Dependency Property stores the property only when it is altered or modified. Hence a huge amount of memory for fields are free. It means that if no value is set for the property then it will return to the inheritance tree up to where it gets the value.

What is dependency property in UWP?

A dependency property represents or supports a specific feature of the programming model for defining a Windows Runtime app with XAML for UI and C#, Microsoft Visual Basic or Visual C++ component extensions (C++/CX) for code. These features include: Data binding.


1 Answers

I believe all you need to do in order to restrict the target type of attached properties is change the definitions of the GetPropertyName and SetPropertyName methods.

Example:

public static int GetAttachedInt(MyTargetType obj)
{
    return (int)obj.GetValue(AttachedIntProperty);
}

public static void SetAttachedInt(MyTargetType obj, int value)
{
    obj.SetValue(AttachedIntProperty, value);
}

where MyTargetType can be any type inheriting from DependencyObject.

like image 164
Noldorin Avatar answered Nov 06 '22 16:11

Noldorin