0

My UserControl code looks like below:

public partial class TriggerListAdd : UserControl
{
    public TriggerListAdd()
    {
        //GetOutputFromInput = (s) => { return s + " " + s; };
        Rows = new ObservableCollection<Row>();
        Rows.CollectionChanged += RowCollectionChanged;
        LoadRows();
        InitializeComponent();
    }

    // ....
    public DependencyProperty GetOutputFromInputProperty = DependencyProperty.Register("GetOutputFromInput", typeof(Func<string, string>), typeof(TriggerListAdd));
    public Func<string, string> GetOutputFromInput { get; set; }
    // ....
}

What I try to achieve is something like:

    public MainWindow()
    {
        TriggerListAddControl.GetOutputFromInput = GetOutputFromInputDefinition;
        InitializeComponent();
    }

or

    <customControl:TriggerListAdd x:Name="TriggerListAddControl" Margin="10,10,10,10" GetOutputFromInput="GetOutputFromInputDefinition"/>

where GetOutputFromInputDefinition is a delegate defined in parent window.

Second method compiles but throw exception after running and first method says TriggerListAddControl is not an object. Do you know how can I define and pass some delegate to user control from parent window? Commented line in user control's constructor is doing the same but I like to pass it from the parent which have references to some external service that will be called there.

  • 1
    Your first approach fails with `NullReferenceException`. See marked duplicate for advice you can use in the future to debug such scenarios yourself. In the meantime, note that the `TriggerListAddControl` field is initialized in the `InitializeComponent()` method, so of course it's `null` if you try to use it _before_ call that method. Move the statement so that it occurs _after_ the call to `InitializeComponent()` and you will be able to assign the property. Which is not to say that this is a reasonable way to do whatever it is you're trying to do, but at least it will work. – Peter Duniho Apr 11 '20 at 00:00
  • Thanks for the valuable input. My intension is to pass some delegate that will be used internally by the control to get second row values from first one... Looking for best approach because as I know ATM there is no possibility to have parametric constructor for usercontrol... – Sebastian Xawery Wiśniowiecki Apr 12 '20 at 22:55

0 Answers0