I have a non-MVVM application. In the MainWindow, I have a TabControl with several tabs, and each tab contains a UserControl. Because those UserControls have similar features, I derive them from a base class that inherits from UserControl. Each of the UserControls has a TextBox called EdiContents. And each of them has a button:
<Button Name="Copy" Content="Copy to Clipboard" Margin="10" Click="Copy_Click" />
I would like to implement Copy_Click in the base UserControl class:
private void Copy_Click(object sender, RoutedEventArgs e)
{
System.Windows.Forms.Clipboard.SetText(EdiContents.Text);
}
But the base class doesn’t know EdiContents TextBox, which is declared in each UserControl’s XAML. Could you please suggest how this can be solved?
Thanks.
You can do something like this.
Note that you are calling
BaseInitComponentafterInitializeComponentXAML behind for derived control
In your BaseUserControl::BaseInitComponent you simply lookup the button by name and wire up the event.