Caliburn.micro 数据网格删除按钮

Caliburn.micro datagrid deleted button

我在学习Caliburn.Micro。我正在将数据从数据库存储到 DataGrid 中。我能够访问数据,但无法从 CaliBurn.Micro 中的 DataGrid 中删除数据。在我的项目中,我想在每一行中都有删除按钮,这样每当我单击特定行的按钮时,该行数据就会被删除。我试过了,但没有用。

这是我的代码

ShellView.xaml

 <Button x:Name="ButtonUpdate" Width="100" Height="30" Content="Update"/>
<DataGrid  x:Name="Empdata2" AutoGenerateColumns="False" ItemsSource="{Binding Path= Empdata}" SelectedItem="{Binding selecteditem}" Height="162" CanUserAddRows="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header=" First Name" Binding="{Binding Path= fname}"/>
            <DataGridTextColumn Header="Last Name" Binding="{Binding Path=lname}"/>
            <DataGridTemplateColumn Header="delete">
                <DataGridTemplateColumn.CellTemplate >
                    <DataTemplate>
                        <Button Content="Delete"  x:Name="DeleteButton"/>
                    </DataTemplate>
                </DataGridTemplateColumn.CellTemplate>
            </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

ShellViewModel.cs

public class ShellViewModel : Caliburn.Micro.PropertyChangedBase, IShell
{
    private string _firstname, _lastname;
    private ObservableCollection<Person> _empdata = new ObservableCollection<Person>();

    public ObservableCollection<Person> Empdata
    {
        get { return _empdata; }
        set
        {
            _empdata = value;
            NotifyOfPropertyChange(() => Empdata);
        }
    }

    Person model = new Person();

    public string FirstName
    {
        get { return _firstname; }
        set { _firstname = value; }
    }
    public string LastName
    {
        get { return _lastname; }
        set { _lastname = value; }
    }
    Auth obj = new Auth();
    public void ButtonUpdate()
    {
        DataSet d = new DataSet();
        d = obj.updateuser();

        for (int i = 0; i < d.Tables[0].Rows.Count; i++)
            Empdata.Add(new Person
            {
                fname = d.Tables[0].Rows[i][0].ToString(),
                lname = d.Tables[0].Rows[i][1].ToString(),
            });
    }

    private Person _selecteditem;
    public Person selecteditem
    {
        get { return _selecteditem; }
        set
        {
            _selecteditem = value;
            NotifyOfPropertyChange(() => selecteditem);
        }
    }

    string sel;

    public void DeleteButton()
    {
        Auth obj = new Auth();
        sel = selecteditem.fname.ToString();
        bool find = obj.deluser(sel);
        if (find == true)
        {
            MessageBox.Show("Deleted");
        }
    }
}

Person.cs

public class Person:PropertyChangedBase
{

    public string FirstName { get; set; }
    public string LastName { get; set; }

    private string Firstname;
    public string fname
    {
        get { return Firstname; }
        set
        {
            Firstname = value;
            NotifyOfPropertyChange(() => fname);
        }
    }
    private string Lastname;
    public string lname
    {
        get { return Lastname; }
        set
        {
            Lastname = value;
            NotifyOfPropertyChange(() => lname);
        }
    }
}

Auth.cs

public class Auth
{
    SqlConnection conn = new SqlConnection(@"Data Source = a; Initial Catalog = ab; Persist Security Info=True;User ID = s; Password=123");

    public DataSet updateuser()
    {
        try
        {

            conn.Open();
            SqlCommand comm = new SqlCommand("Select * from [add]", conn);
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(comm);
            da.Fill(ds);
            conn.Close();
            return ds;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    public bool deluser(string name)
    {
        try
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("Delete from [add] where firstname=" + name + "", conn);
            comm.ExecuteNonQuery();
            conn.Close();
            return true;
        }
        catch(Exception ex)
        {
            throw ex;

        }
    }
}

cal:Bind.Model 附加 属性 设置到您的视图模型:

<DataTemplate xmlns:cal="http://www.caliburnproject.org">
    <Button Content="Press Me" VerticalAlignment="Top" 
            cal:Bind.Model="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}"
            x:Name="DeleteButton" />
</DataTemplate>

那么当点击按钮时应该调用 ShellViewModelDeleteButton 方法。