我正在学习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;
}
}
}
答案 0 :(得分:1)
将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>
然后在点击按钮时应调用DeleteButton
的{{1}}方法。