WPF - Access UserControl from a selected TabItem

0

I have a TabControl with several TabItems , which separated the content in UserControls to organize the code. But all of them have a common TextBox of which I want to get the value in a Button in the MainWindow . I'm new to WPF , I'm still learning, I thought about exposing the TextBox value in a public property on each UserControl , hence then get the TabItem selected ( (TabItem)TabControlMain.SelectedItem ), and get the internal UserControl object to access such property (could use an interface with a Get method). The problem is that I can not get this UserControl , I do not know how to do it.

I'd like to know how to do something like this, or if this is an unsuitable solution to WPF , I'd like to know how I could structure my code for such a case: in> and get internal values from the window that has TabControl , preferably separating by files (as with UserControls ).     

asked by anonymous 08.04.2018 / 20:15

1 answer

1

I solved the problem!

Below is an outline that demonstrates my solution:

var tabSelecionada = (TabItem)TabControlMain.SelectedItem;
var uc = (IInterfaceComGet)tabSelecionada.Content;
var resultado = uc.MétodoGetDaInterface();

And in UserControl:

public partial class MeuUserControl : UserControl, IInterfaceComGet { ... }

Basically, my difficulty was to ignore the Content property.

    
18.04.2018 / 01:21