0

我是 wpf mvvm 的新手。为了在选择时从数据网格中获取值,我创建了一个对象数据类型的属性。当我单击数据网格行时,我得到一组包含名字、姓氏、城市、州的值,pin,mobileno,mail id,employee id 等。我需要从中检索员工 id 用于更新目的..但我不知道如何从对象数据类型中声明的属性中检索这些值..请帮助我.. ..\

这是我的视图模型

public object selectedEmployee;
public object SelectedEmployee
{
    get
    {
        return selectedEmployee;
    }
    set
    {
        selectedEmployee = value;
        OnPropertyChanged("SelectedEmployee");
    }
}

这是我的 xaml 代码

<DataGrid Grid.Row="2" x:Name="grdEmployee" SelectedItem="{Binding SelectedEmployee, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding EmployeeDatatable, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}" AutoGenerateColumns="False"  IsReadOnly="True"  HorizontalAlignment="Center" Margin="59,0,86,12" Width="492" CanUserAddRows="False"  
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Emp_id}" Header="Employee ID"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding LastName}" Header="LastName"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding Age}" Header="Age"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding ZipCode}" Header="ZipCode"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding PhoneNumber}" Header="PhoneNumber"></DataGridTextColumn>
            <DataGridTextColumn Binding="{Binding MobileNumber}" Header="MobileNumber"></DataGridTextColumn>
            <DataGridTemplateColumn>
                <DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Button Name="btnEdit" Content="Edit"></Button>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>

        </DataGrid.Columns>

    </DataGrid>
4

1 回答 1

0

伙计,您必须将某种类型的自定义对象(具有这些属性)放入您的object变量中。您所要做的就是将其转换object回其原始类型,然后您将能够再次访问这些属性。因为我不知道它是什么类型,所以对于这个例子,我将假设你有一个名为的类Employee,它具有这些属性。这是您投射对象的方式:

Employee employee = (Employee)yourRowObject;
// Now you can access the properties. For example...
string fullName = string.Concat(employee.Firstname, " ", employee.LastName);
int employeeId = employee.Id;
于 2013-09-17T08:29:19.850 回答