I have successfully created a DataGrid with WPF (C#) and bonded the buttons...However I now would like to bind a different event to each individual button instead. I plan to rename the buttons and open different file locations on my PC.
C#
wpfCrudEntities _db = new wpfCrudEntities();
public static DataGrid datagrid;
public MainWindow()
{
InitializeComponent();
Load();
}
private void Load()
{
myDataGrid.ItemsSource = _db.members.ToList();
datagrid = myDataGrid;
}
private void insertBtn_Click(object sender, RoutedEventArgs e)
{
InsertPage Ipage = new InsertPage();
Ipage.ShowDialog();
}
private void updateBtn_Click(object sender, RoutedEventArgs e)
{
int Id = (myDataGrid.SelectedItem as member).id;
UpdatePage Upage = new UpdatePage(Id);
Upage.ShowDialog();
}
private void deleteBtn_Click(object sender, RoutedEventArgs e)
{
int Id = (myDataGrid.SelectedItem as member).id;
var deleteMemeber = _db.members.Where(m => m.id == Id).Single();
_db.members.Remove(deleteMemeber);
_db.SaveChanges();
myDataGrid.ItemsSource = _db.members.ToList();
}
}
XAML
<Window x:Class="wappppa.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:wappppa"
mc:Ignorable="d"
Title="MainWindow" Height="600" Width="600">
<DockPanel>
<DataGrid x:Name="myDataGrid" x:FieldModifier="public" AutoGenerateColumns="False" ColumnWidth="*" >
<DataGrid.Columns>
<DataGridTextColumn Header="Id" Binding="{Binding id}" />
<DataGridTextColumn Header="Name" Binding="{Binding name}" />
<DataGridTextColumn Header="Gender" Binding="{Binding gender}" />
<DataGridTemplateColumn>
<DataGridTemplateColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Action" />
</DataTemplate>
</DataGridTemplateColumn.HeaderTemplate>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button x:Name="insertBtn" Content="Insert" Click="insertBtn_Click"/>
<Button x:Name="updateBtn" Content="Update" Click="updateBtn_Click"/>
<Button x:Name="deleteBtn" Content="Delete" Click="deleteBtn_Click"/>
</StackPanel>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Window>