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 Dias
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
– Emerson
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.
– Nicolas Dias