2012-12-07 171 views
4

所以,我有一個TabControl綁定到項目列表(每個選項卡是一個項目) - 工作正常。每個選項卡的內容都是一個帶有項目員工列表的DataGrid - 工作良好。現在,我想顯示一些關於DataGrid上當前選擇的員工的信息。下面是一些代碼: MainWindow.xaml文件:WPF綁定DataGrid中SelectedItem

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

,後來,我想簡單地在標籤寫它來測試這種結合:

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

和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)); 
     } 
    } 

我是WPF的新手,我已經閱讀了一些技巧,但他們沒有幫助。標籤'emp'沒有顯示任何內容。我錯過了什麼?

回答

7

您還沒有通知你的屬性已更改,試試這個

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

測試:

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

代碼:

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; } 
    } 

這似乎達到預期效果,或者是我錯過了什麼?

+0

謝謝,那只是我的錯誤,我的意思是寫「SelectedEmployee」。我改變了它,但它沒有幫助。 – kcz

+0

我做了一些測試,但似乎工作,但我沒有你的XAML持有DataGrid模板。 –