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方法。

相关内容

  • 没有找到相关文章

最新更新