2
votes

I have custom control (FooCtrl) and it has 4 DependencyProperties (Q1, Q2, Q3 and Q4).

public partical class FooCtrl : UserControl { 
    // ... 

    // Q1:
    public static readonly DependencyProperty Q1Property =
    DependencyProperty.Register("Q1", typeof(UIElement), typeof(FooCtrl), new UIPropertyMetadata(null));
    public UIElement Q1
    {
      get { return (UIElement)GetValue(Q1Property); }
      set { SetValue(Q1Property, value); }
    }

    // Q2:
    public static readonly DependencyProperty Q2Property =
    DependencyProperty.Register("Q2", typeof(UIElement), typeof(FooCtrl), new UIPropertyMetadata(null));
    public UIElement Q2
    {
      get { return (UIElement)GetValue(Q2Property); }
      set { SetValue(Q2Property, value); }
    }

    // Q3:
    // ....

    // Q4:
    // ....


    public void DoSomething() 
    {
        // ...
    }
}

I use the control like:

<my:FooCtrl>
<my:FooCtrl.Q1>
    <ContentControl Content="{Binding ...}" /> 
</my:FooCtrl.Q1>
<my:FooCtrl.Q2>
    <ContentControl Content="{Binding ...}" /> 
</my:FooCtrl.Q2>
<my:FooCtrl.Q3>
    <ContentControl Content="{Binding ...}" /> 
</my:FooCtrl.Q3>
<my:FooCtrl.Q4>
    <ContentControl Content="{Binding ...}" /> 
</my:FooCtrl.Q4>

I can add 4 usercontrols to the FooCtrl Q1, Q2, Q3 and Q4 properties. Via the method "DoSomething()" on the FooCtrl I can change animate Q1, Q2, Q3 and Q4:

myFooCtrl.DoSomething();

How can I access the method from FooCtrl "DoSomething()" from inside my usercontrols (which are attached via Q1, Q2, Q3, Q4)? E.g. if I bind a usercontrol to a contentcontrol of Q1 - Q4 and the usercontrol contains a button "Do it!" how can this access the "DoSomething()" from FooCtrl so that it gets executed?

If it helps: I'm also using Caliburn.Micro.

1

1 Answers

0
votes

If DoSomething() is truly only view based, in that it just drives an animation, and that animation doesn't represent any kind of model state, then you can use a RoutedCommand.

Your FooControl can expose a custom routed command, and your "Do it!" buttons can bind to it. Routed commands will travel up and down the visual tree until they find something that has a matching command binding. In your case they will bubble up the tree until they hit your FooControl, which will then execute the command.

Another option would be to have FooControl expose Attached Properties that the buttons can bind to. Much like stuff inside a Grid can use Grid.Row to tell their parent where they want to live.

If the animations DO represent some kind of model state, then the animations should be driven from the ViewModel bindings.