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
. >