Wpf 数据绑定自定义对象 公共类员工:INotifyPropertyChanged { //私有财产 私有字符串\u名称; 私人名单地址; //适当实现getter和setter的公共属性(引发OnPropertyChanged事件) } 公共类EmployeeRecords:ObservableCollection { 公共事业部员工(员工) { 添加(员工); 返回true; } 公共bool RemoveEmployee(内部索引) { 如果(计数>0) { RemoveAt(index);//与执行“RemoveAt(SelectedIndex);”相同 } 返回true; } } **XAML:** 名称 埃皮德 任命 地址 MainWindow.xaml.cs: 公共部分类主窗口:窗口 { 私人雇员记录; 公共主窗口() { 初始化组件(); 初始化(); lstEmployee.ItemsSource=\u empRecords; } . . . . }

Wpf 数据绑定自定义对象 公共类员工:INotifyPropertyChanged { //私有财产 私有字符串\u名称; 私人名单地址; //适当实现getter和setter的公共属性(引发OnPropertyChanged事件) } 公共类EmployeeRecords:ObservableCollection { 公共事业部员工(员工) { 添加(员工); 返回true; } 公共bool RemoveEmployee(内部索引) { 如果(计数>0) { RemoveAt(index);//与执行“RemoveAt(SelectedIndex);”相同 } 返回true; } } **XAML:** 名称 埃皮德 任命 地址 MainWindow.xaml.cs: 公共部分类主窗口:窗口 { 私人雇员记录; 公共主窗口() { 初始化组件(); 初始化(); lstEmployee.ItemsSource=\u empRecords; } . . . . },wpf,binding,observablecollection,Wpf,Binding,Observablecollection,我试图在第一个列表框中显示雇员的所有属性,而在第二个列表框中仅显示地址。有人能告诉我我做错了什么吗 由于我无法显式访问EmployeeRecords中的Employee对象,如何将Employee中的地址列表绑定到控件 提前谢谢! Shanks假设您希望在第二个列表框中显示所选员工的地址,则以下操作应该有效 public class Employee : INotifyPropertyChanged { // Private Properties

我试图在第一个列表框中显示雇员的所有属性,而在第二个列表框中仅显示地址。有人能告诉我我做错了什么吗

由于我无法显式访问EmployeeRecords中的Employee对象,如何将Employee中的地址列表绑定到控件

提前谢谢!
Shanks

假设您希望在第二个
列表框
中显示所选员工的地址,则以下操作应该有效

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

注:

我假设支持字段的公共财产名称
私有列表\u地址
地址

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