WPF Binding SelectedItem in DataGrid

So, I have a TabControl tied to a list of projects (each tab is a single project) - this works great. The contents of each tab is a DataGrid with a list of project staff - this also works well. Now I want to show some information about the user selected in the DataGrid. Here is the code: MainWindow.xaml file:

<Window.Resources> <DataTemplate x:Key="ItemTemplate"> <TextBlock Text="{Binding Name}" /> </DataTemplate> <DataTemplate x:Key="ContentTemplate"> <DataGrid ItemsSource="{Binding Employees}" SelectedItem="{Binding SelectedEmployee, Mode=TwoWay}" SelectionMode="Extended" SelectionUnit="FullRow" Name="employeesList"> </DataGrid> </DataTemplate> </Window.Resources> 

and later, I want to check this binding by simply writing it in the label:

 <Label Name="emp" Content="{Binding SelectedEmployee}"></Label> 

and MainWindowViewModel:

  public Employee SelectedEmployee { get { return selectedEmployee; } set { if (selectedEmployee != value) { selectedEmployee = value; NotifyPropertyChanged("SelectedEmployee"); } } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } 

I'm kind of new to WPF, I read some tips, but they do not help. The label "emp" does not show anything. What am I missing?

+4
source share
1 answer

You do not notify that your property has changed, try

 public Employee SelectedEmployee { get { return selectedEmployee; } set { if (selectedEmployee != value) { selectedEmployee = value; LastName = value; NotifyPropertyChanged("SelectedEmployee"); //NotifyPropertyChanged("SelectedItem"); } } } 

Test:

 <Window x:Class="WpfApplication6.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication6" Title="MainWindow" Height="350" Width="763" Name="UI" > <Window.Resources> <DataTemplate x:Key="ItemTemplate"> <TextBlock Text="{Binding Name}" /> </DataTemplate> </Window.Resources> <Grid> <DataGrid ItemsSource="{Binding ElementName=UI,Path=Employees}" SelectedItem="{Binding ElementName=UI,Path=SelectedEmployee}" SelectionMode="Extended" SelectionUnit="FullRow" Name="employeesList" Margin="0,41,0,0" /> <Label Content="{Binding ElementName=UI,Path=SelectedEmployee.Name}" Height="28" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" Width="288" /> <Label Content="{Binding ElementName=employeesList,Path=SelectedItem.Name}" Height="28" HorizontalAlignment="Left" Name="label2" VerticalAlignment="Top" Width="288" Margin="294,0,0,0" /> </Grid> </Window> 

code:

  public partial class MainWindow : Window, INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; private ObservableCollection<Employee> _employees = new ObservableCollection<Employee>(); private Employee _selectedEmployee; public MainWindow() { InitializeComponent(); Employees.Add(new Employee { Name = "sa_ddam213" }); } public ObservableCollection<Employee> Employees { get { return _employees; } set { _employees = value; } } public Employee SelectedEmployee { get { return _selectedEmployee; } set { _selectedEmployee = value; NotifyPropertyChanged("SelectedEmployee"); } } /// <summary> /// Notifies the property changed. /// </summary> /// <param name="info">The info.</param> public void NotifyPropertyChanged(String info) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(info)); } } } public class Employee { public string Name { get; set; } } 

Does this seem to work as expected, or am I missing something?

+7
source

All Articles