Information from a User Control to the parent window

1

I'm trying to send some info from my User Control to the title bar of the main screen that would be the parent window. The information I'm trying to send is the selected company of a DataGrid.

How can I do this? I'm not getting.. I look forward to any help ...

Class where I store captured information;

public class EmpresaSelecionada
    {
        public int Codigo { get; set; }
    }

    public int Codigo
    {
        get { return es.Codigo; }
        set { es.Codigo = value; }
    }

Where do I record:

es = new EmpresaSelecionada();

Object item = dataGridEmpresa.SelectedItem;

string codigo = (dataGridEmpresa.SelectedCells[0].Column.GetCellContent(item) as TextBlock).Text;

es.Codigo = Convert.ToInt32(codigo);

Where I try to retrieve to play in my main screen TextBlock:

Telas.EscolherEmpresa.EmpresaSelecionada es = new Telas.EscolherEmpresa.EmpresaSelecionada();

    public int Codigo = 0;

    public MainWindow()
    {
        InitializeComponent();

        es = new Telas.EscolherEmpresa.EmpresaSelecionada();
        Codigo = es.Codigo;
    }

    private string _titulo;// = Convert.ToString(Codigo);
    public string Titulo
    {
        get
        {
            return _titulo;
        }
        set
        {
            _titulo = Convert.ToString(Codigo);
        }
    }

XAML

<TextBlock Text="{Binding Titulo,RelativeSource={RelativeSource FindAncestor,AncestorType=Window}}" HorizontalAlignment="Center"  />
    
asked by anonymous 19.06.2015 / 18:50

1 answer

1

To do this elegantly, I've set an example for you. I will describe in parts:

First of all in your project you should have a template for Empresa , below is the basic template that I created to exemplify:

public class Empresa
{
    public int Codigo { get; set; }
    public string RazaoSocial { get; set; }
    public string Fantasia { get; set; }

    public override string ToString()
    {
        return String.Format("{0} - {1}",Codigo,Fantasia);
    }
} 

After that, I created an example UserControl called MeuUserControl . Below the% code of MeuUserControl.xaml :

<UserControl x:Class="WpfApplication1.MeuUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" >
    <Grid>
        <DataGrid x:Name="dg_empresas" AutoGenerateColumns="False"
                  SelectedItem="{Binding EmpresaSelecionada,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl},Mode=TwoWay}" 
                  ItemsSource="{Binding Empresas, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl}}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Codigo" Binding="{Binding Codigo}"/>
                <DataGridTextColumn Header="Razão social" Binding="{Binding RazaoSocial}"/>
                <DataGridTextColumn Header="Fantasia" Binding="{Binding Fantasia}"/>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</UserControl>

Following the MyUserControl.cs% (I've created a list with hypothetical companies):

  public partial class MeuUserControl : UserControl
    {
        public static readonly DependencyProperty EmpresaSelecionadaProperty =
                               DependencyProperty.Register("EmpresaSelecionada", typeof(Empresa), typeof(MeuUserControl), new PropertyMetadata(null));

        public Empresa EmpresaSelecionada
        {
            get{ return this.GetValue(EmpresaSelecionadaProperty) as Empresa; }
            set{ this.SetValue(EmpresaSelecionadaProperty, value); }
        }

        public List<Empresa> Empresas 
        {
            get 
            {
                return new List<Empresa>()
                {
                    new Empresa(){Codigo=1, RazaoSocial= "Razao Social 1", Fantasia="Fantasia 1"},
                    new Empresa(){Codigo=2, RazaoSocial= "Razao Social 2", Fantasia="Fantasia 2"},
                    new Empresa(){Codigo=3, RazaoSocial= "Razao Social 3", Fantasia="Fantasia 3"},
                    new Empresa(){Codigo=4, RazaoSocial= "Razao Social 4", Fantasia="Fantasia 4"},
                };
            }
        }

        public MeuUserControl()
        {
            InitializeComponent();
        }
    }

And finally the% w of% of the screen that uses XAML , whose title is the company selected in CodeBehind :

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:controls="clr-namespace:WpfApplication1"
        Title="{Binding EmpresaSelecionada,ElementName=userControl,Mode=OneWay}">
    <Grid>
        <controls:MeuUserControl x:Name="userControl"/>
    </Grid>
</Window>

The solution basically boils down to creating a XAML in UserControl that is bound to DataGrid selected in DependencyProperty , and bind this property to the title of the window using UserControl . >     

03.07.2015 / 03:11