Kamil
Kamil

Reputation: 13941

Add logic to control with DependencyProperty

I need to add some logic to user control with DependencyProperty. My logic is supposed to change properties on controls inside my UserControl.

I want to avoid building huge "dependency tree" because I have a lot of user controls. I just want to use binding in my windows (not in nested user controls).

This is my control:

public partial class BucketElevatorControl : UserControl
{
    public BucketElevatorControl()
    {
        InitializeComponent();
    }

    public bool On
    {
        get
        {
            return (bool)GetValue(OnProperty);
        }
        set
        {
            SetValue(OnProperty, value);
        }
    }

    // Using a DependencyProperty as the backing store for IsOn.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty OnProperty = DependencyProperty.Register(
            "On",
            typeof(bool),
            typeof(BucketElevatorControl),
            new PropertyMetadata(
                false, PropertyChangedCallback
                ));



    private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
    {
        // I want to do something with my UserControl child controls
    }
}

The question is: how can I do some logic in contol code behind and take advantage of data binding?

My logic is complicated (drawing graphics, animations etc.).

Upvotes: 0

Views: 127

Answers (2)

I have no idea what you mean by "dependency tree", but if you want to alter the state of stuff in your template according to changes in your control's dependency properties, you can do that with TemplateBinding and/or triggers in your control template. Write value converters if you need to. Most of what you need to do can probably be done that way.

If you need more complicated logic, you can also override OnApplyTemplate() in your control, and call GetTemplateChild() to get named controls within the control's template. For example, you might require the template to have a TextBox somewhere in it called PART_FooText; throw an exception if you get null from GetTemplateChild("PART_FooText") as TextBox. If the TextBox is there, do anything you like to it: Handle events, set properties, etc. If you like, keep a private field TextBox _PART_FooText; to fiddle with it later on, in your property-changed callbacks, other events, or whatever.

Upvotes: 0

leetibbett
leetibbett

Reputation: 843

You should create CoerceValueCallbacks for the properties you want to change. Those callbacks set the new values. When this property changes, you then coerce the others, like so:

private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)

{
  dependencyObject.CoerceValue(MinReadingProperty);
  dependencyObject.CoerceValue(MaxReadingProperty);
}

Upvotes: 1

Related Questions