Pass parameter to constructor in UserControl

0

Very good, I have an application in which I should create as many UserControl objects as a value stored in a ComboBox variable. The objects are created but I would like that each time you create a new one, increase the value of your counter in one.

The problem is that I do not know how to pass that number to XAML ... In this image I think it will understand what I ask;

In the image, as I need 4 objects I create them automatically but they all start from the same class and have the same Header , that is, they are all called Configuracion nivel 0

Can I do in some way that each object that I create has a correlative number? 0,1,2,3,4,5 ...

This XAML thing is killing me ...

Thanks

    
asked by Edulon 24.12.2017 в 12:18
source

1 answer

0

You do not know the structure of the object you create, but you can add a method called setCount (int) where when you add that control to the view, you assign the counter to TextBlock .

For example let's say that the next class is the one you add to the control when you add a level:

public class ConfigNivelControl : UserControl
{
    public ConfigNivelControl(){
      InitializeComponents();
    }

    public void SetCounter(int counter)
    {
       TextBlockContador.Text = counter.ToString();
    }
   // tu codigo...
}

Then when you initialize it and add it to the control, use the instance and send it the counter:

ConfigNivelControl control = new ConfNivelControl();
// agregas el control al otro control...
//...
//...

// le asignamos el contador:
control.SetContador(variableContador);
    
answered by 25.12.2017 в 13:55