I have succesfully 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>
Ignore:
It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more details.It looks like your post is mostly code; please add some more
CodePudding user response:
Using the MVVM pattern you define a ViewModel for your rows.
Then you can use a CommandBinding to bind the Button Click event to the ViewModel of the respective row.
In your case this would be the Member class that needs to be wrapped in a MemberViewModel where the 3 coammnds are defined.
CodePudding user response:
When a button click event fired you can access convert the sender object to a button and do whatever you want.
private void Button_Click(object sender, RoutedEventArgs e)
{
var clickedButton= (Button)sender;
// Do whatever you want
}
CodePudding user response:
You should be able to do something like this:
private void insertBtn_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button)
{
var dataContex = button.DataContext;
// This is your item for that row.
// You just need to cast it if you want to use it.
InsertPage Ipage = new InsertPage();
Ipage.ShowDialog();
}
}