Information from a User Control to the Parent Window

Asked

Viewed 561 times

1

I’m trying to send some information 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 company selected from a Datagrid.

How can I do that? I’m not getting it.. I expect 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 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 recover to play on 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"  />

1 answer

1


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

First in your project you should have a model for Empresa, below is the basic model 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 a UserControl example called MeuUserControl. Below the code XAML 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 CodeBehind of Meuusercontrol.Cs (I created a list of 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 XAML of the screen using the UserControl, whose title is the company selected in DataGrid:

<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 DependencyProperty in the UserControl which is connected with the Empresa selected in the DataGrid, and link this property to the window title using UserControl.

  • This solution has been useful for you?

  • Nicolas, but how can I call this company and play for the title? I tried with two clicks on the item of Datagrid.. But I do not know how to call it

  • As you can notice the screen title is connected with the selected datagrid item, so with this solution, whenever the item selected in Datagrid changes, the title automatically changes.. Try to reproduce the above example in a new project, which you can understand the solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.