在wpf和mvvm模式中进行控制

时间:2018-08-01 08:26:42

标签: c# wpf xaml mvvm

我该如何在wpf中设计控件,使得在单击更新按钮时,焦点应位于特定行上,并应显示相应的标签。 请找到所附的屏幕截图!

enter image description here

DataGrid x:Name="dataEmployee" HorizontalAlignment="Left" Margin="76,71,0,0" IsReadOnly="True" VerticalAlignment="Top" Height="121" Width="305" AutoGenerateColumns="False"> 
    <DataGrid.Columns> 
        <DataGridTextColumn Header="Employee Id" Binding="{Binding EmployeeId}"/> 
        <DataGridTextColumn Header="Employee Name" Binding="{Binding EmployeeName}"/> 
        <DataGridTextColumn Header="Salary" Binding="{Binding EmployeeSalary}"/> 
        <DataGridTextColumn Header="Designation" Binding="{Binding EmployeeDesignation}"/>
    </DataGrid.Columns>
</DataGrid>

1 个答案:

答案 0 :(得分:0)

您可以直接绑定到DataGrid

<StackPanel>
    <DataGrid x:Name="dataEmployee"  IsReadOnly="True" VerticalAlignment="Top" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Employee Id" Binding="{Binding EmployeeId}"/>
            <DataGridTextColumn Header="Employee Name" Binding="{Binding EmployeeName}"/>
            <DataGridTextColumn Header="Salary" Binding="{Binding EmployeeSalary}"/>
            <DataGridTextColumn Header="Designation" Binding="{Binding EmployeeDesignation}"/>
        </DataGrid.Columns>
    </DataGrid>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Name"/>
        <TextBox Text="{Binding ElementName=dataEmployee, Path=SelectedItem.EmployeeName}"/></StackPanel>
</StackPanel>

但是由于规范说您应该按下一个按钮,所以我们也要这样做:

XAML几乎相同:

<StackPanel>
    <DataGrid x:Name="dataEmployee"  IsReadOnly="True" VerticalAlignment="Top" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Employee Id" Binding="{Binding EmployeeId}"/>
            <DataGridTextColumn Header="Employee Name" Binding="{Binding EmployeeName}"/>
            <DataGridTextColumn Header="Salary" Binding="{Binding EmployeeSalary}"/>
            <DataGridTextColumn Header="Designation" Binding="{Binding EmployeeDesignation}"/>
        </DataGrid.Columns>
    </DataGrid>
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="Name"/>
        <TextBox Name="currentEmployeeName"/></StackPanel>
    <Button Height="30" Content="Update" Click="Button_Click"/>
</StackPanel>

然后我创建了一个简单的Class来处理数据:

public class Employee
{

    public string EmployeeId { get; set; }
    public string EmployeeName { get; set; }
    public string EmployeeSalary { get; set; }
    public string EmployeeDesignation { get; set; }
}

其余部分并不困难,但是当然必须根据您的DataModel有所不同。重要的是Button_Click事件:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<Employee> listEmployees = new List<Employee>();

        listEmployees.Add(new Employee() { EmployeeName = "Bob" });
        listEmployees.Add(new Employee() { EmployeeName = "Mary" });
        listEmployees.Add(new Employee() { EmployeeName = "Kato" });
        listEmployees.Add(new Employee() { EmployeeName = "Mohammed" });

        dataEmployee.ItemsSource = listEmployees;
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        Employee currentEmployee = dataEmployee.SelectedItem as Employee;

        if (currentEmployee == null)
        {
            currentEmployeeName.Text = "no selection";
        }
        else
        {
            currentEmployeeName.Text = currentEmployee.EmployeeName;
        }
     }
}