2013-08-06 228 views
0
public class Employee : INotifyPropertyChanged 
    { 
     // Private Properties 
     private string _name; 
     private List<string> _address; 

     // The Public properties for which the getters and setters are implemented appropriately (raising OnPropertyChanged event) 
    } 

    public class EmployeeRecords: ObservableCollection<Employee> 
    { 
     public bool AddEmployee(Employee employee) 
     { 
      Add(employee); 
      return true; 
     } 

     public bool RemoveEmployee(int index) 
     { 
      if (Count > 0) 
      { 
       RemoveAt(index); // is the same as doing 'RemoveAt(SelectedIndex);' 
      } 

      return true; 
     } 
    } 

    **XAML:** 

<Window x:Class="EmployeeInfo.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:src="clr-namespace:EmployeeInfo" 
     Name="Page1" Height="225" Width="610" 
     Title="DATABINDING DEMO"> 
    <Window.Resources> 
     <DataTemplate x:Key="EmployeeTemplate"> 
      <StackPanel> 
       <TextBlock Text="{Binding Path=Name}" /> 
       <TextBlock Text="{Binding Path=EmpId}"/> 
       <TextBlock Text="{Binding Path=Designation}"/> 
      </StackPanel> 
     </DataTemplate> 
     <src:EmployeeRecords x:Key="EmpInfo"/> 
    </Window.Resources> 
    <Window.DataContext> 

     <Binding Source="{StaticResource EmpInfo}"/> 
    </Window.DataContext> 

    <Canvas Height="190" Width="600"> 
     <Label Name="lblName" Height="30" Width="50" Canvas.Top="0" Canvas.Left="0" >Name</Label> 
     <TextBox Name="txtBoxName" Height="30" Width="125" Text="{Binding Path=Name}" Canvas.Top="5" Canvas.Left="60" /> 

     <Label Name="lblEmpId" Height="30" Width="50" Canvas.Top="40" Canvas.Left="0" >EmpId</Label> 
     <TextBox Name="txtBoxEmpId" Height="30" Width="50" Text="{Binding Path=EmpId}" Canvas.Top="40" Canvas.Left="60" /> 

     <Label Name="lblDesignation" Height="30" Width="50" Canvas.Top="75" Canvas.Left="0" >Designation</Label> 
     <TextBox Name="txtBoxDesignation" Height="30" Width="50" Text="{Binding Path=Designation}" Canvas.Top="75" Canvas.Left="60" /> 

     <Label Name="lblAddress" Height="30" Width="50" Canvas.Top="115" Canvas.Left="0" >Address</Label> 
     <TextBox Name="txtBoxAddress" Height="30" Width="50" Text="{Binding Path=Address}" Canvas.Top="115" Canvas.Left="60" /> 
     <TextBox Name="txtBoxAddress2" Height="30" Width="50" Text="{Binding Path=Address}" Canvas.Top="115" Canvas.Left="120" /> 

     <Button Height="30" Width="50" Canvas.Top="155" Canvas.Left="0" Content="Update" Click="UpdateButton_Click"/> 
     <Button Height="30" Width="50" Canvas.Top="155" Canvas.Left="60" Content="Submit" Click="SubmitButton_Click"/> 
     <Button Height="30" Width="50" Canvas.Top="155" Canvas.Left="120" Content="Remove" Click="RemoveButton_Click" /> 

     <ListBox SelectedIndex="{Binding SelectedIndex}" 
       MouseDoubleClick="LstEmployee_MouseDoubleClick" Name="lstEmployee" Height="180" Width="190" 
       Canvas.Top="5" Canvas.Left="200" ItemsSource="{Binding}" ItemTemplate="{StaticResource EmployeeTemplate}"/> 

     <ListBox Name="listAddress" Height="180" Width="190" Canvas.Top="5" Canvas.Left="400" ItemsSource="{Binding Path=/Address}"/> 
    </Canvas> 
</Window> 

MainWindow.xaml.cs: 
    public partial class MainWindow : Window 
    { 
     private EmployeeRecords _empRecords; 

     public MainWindow() 
     { 
      InitializeComponent(); 
      Initialize(); 

      lstEmployee.ItemsSource = _empRecords; 
     } 
     . 
     . 
     . 
     . 
    } 

我試圖顯示在第一個列表框中的員工的所有屬性,只是在第二個列表框中的地址。有人能告訴我我在這裏做錯了嗎?數據綁定自定義對象

由於我無法顯式訪問EmployeeRecords中的Employee對象,因此如何將Employee內的Address列表綁定到控件?

在此先感謝! Shanks

回答

0

假設您想在第二個ListBox中顯示選定員工的地址,以下內容應該可以工作。

<ListBox Grid.Row="1" Name="listAddress" Height="180" Width="190" Canvas.Top="5" Canvas.Left="400" ItemsSource="{Binding ElementName=lstEmployee, Path=SelectedItem.Address}"/> 

注:

我假定爲後盾場private List<string> _address;公共屬性名稱是Address