I have my custom control MyControl, which has a public method Start().
public partial class MyControl : UserControl
{
// This must be private.
private int _idNumber;
public MyControl()
{
InitializeComponent();
}
public void Start(int idNumber)
{
_idNumber = idNumber;
}
}
In the MainWindow, I put one MyControl with x:Name="myControl".
<Window x:Class="MyNameSpace.MainWindow"
xmlns:local="clr-namespace:MyNameSpace">
<Grid>
<local:MyControl x:Name="myControl"/>
</Grid>
</Window>
In the Start method of the MainWindow, I call the Start method of MyControl using x:Name.
public partial class MainWindow : Window
{
// This must be private
private int _myContolId;
public MainWindow()
{
InitializeComponent();
}
public void Start()
{
// ID must be set here.
_myControlId = 1;
myControl.Start(_myControlId);
}
}
How can I do the same thing without using x:Name?
Note that Loaded event of MyControl is ineffective in my case, since the Start() method of MyControl MUST be called before it is loaded as a visual element.
It is also ineffective to call Start in the constructor of MyControl or when it is initialized, since the int argument idNumber must be set in the Start method of MainWindow.
What is more, both _idNumber of MyControl and _myContolId of MainWindow must be private, for both setter and getter.